There is the next class

class Json { private $file; private $json; function __construct() { $this->$file = __DIR__ . '\..\file.json'; $this->$json = json_decode(file_get_contents($this->$file)); } public function change($namespace, $object, $value) { $this->$json->$namespace->$object->text = $value; $f = fopen($this->$file, 'w'); fputs($f, json_encode($this->$json)); fclose($f); return true; } } 

When calling the change function

 $JSON = new Json(); $JSON->change('section', 'par', 'value'); 

Errors appear

Java.php on line 14 Warning: fopen ()

Warning: given to json.php on line 15

Warning: fclose (), given in json.php on line 16

How can this be fixed?

    1 answer 1

    Non-static properties inside a class should be addressed as follows: $this->file , $this->json (without the $ sign in front of the name).

     class Json { private $file; private $json; function __construct() { $this->file = __DIR__ . '\..\file.json'; $this->json = json_decode(file_get_contents($this->file)); } public function change($namespace, $object, $value) { $this->json->$namespace->$object->text = $value; file_put_contents($this->file, json_encode($this->json)); return true; } }