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 !