0

Can anyone help me clear this issue ?
I have a JsonMaker class as:

class JsonMaker {

    protected $jsonObj;
    protected $path;

    function __construct($filepath) {

        $this->path = $filepath;

        $str = file_get_contents($this->path, true);
        $this->jsonObj = json_decode($str, false);

    }

    public function Test_1($rootName){

        $file = file_get_contents($filepath, true);
        $data = json_decode($file, false);

        foreach ($data->{$rootName} as $key => $value){

            echo $value->ID;         //works fine !
        }
    }

    public function Test_2(){ 

        foreach ($this->jsonObj->{$rootName} as $key => $value){

            echo $value->ID;         
        }
    }    
}

upon test run, as in:

$var = JsonMaker("blabla.json");
$var->Test_1("People");        //works fine
$var->Test_2("People");        //Failed

the honest truth is,i actually prefer manipulating
my data with the $this->jsonObj property; rather than call
$data = json_decode() to use $data object each time any Json
method is called. but why is $var->Test_2() failing with a
warning message ? or am i doing the wrong thing ? any explanation
to this misfortune will be highly regarded. thanks !

4
Contributors
5
Replies
17
Views
2 Years
Discussion Span
Last Post by broj1
0
 foreach ($this->jsonObj->{$rootName} as $key => $value){

Method (Test_2) is looking for $rootName but this is not passed to the method as a parameter so it doesn't exist.

0

In the Test_2 method the $rootName is not defined so it won't work.

Anyway, I do not see any added value in your approach. You could do all this just by using json_decode. But if you really want to do it through your custom class then you should have good reasons for that such as provide some custom validations, provide various types of output etc. In the above class i.e. you should check if file exists in the constructor, maybe use setters and getters to set paths and get values etc.

0

Thanks Diadol ! i did passed $rootName to Test_2. even. i hastily didnt just add it to the thread. so whats's the way out ? @ broj1. i did all of that as u said. what i've given out is just an metal picture of the actual code in question. trust me, i've done what is proper but still getting :

Notice: Trying to get property of non-object in C:\Server\wamp\www\Oweb\libs\blabla.php on line bla bla

Edited by Osagie_1

0

As said in previous posts: correct the Test_2 method:

public function Test_2($rootName){
    foreach ($this->jsonObj->{$rootName} as $key => $value){
        echo $value->ID;
    }
}

and initialize the $var object before calling methods:

    $var = new JsonMaker("blabla.json");
    $var->Test_1("People");
    $var->Test_2("People");

And it would be wise to add some check for values before using them e.g.:

if(isset($this->jsonObj->{$rootName})) {
...    
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.