I have code like this, that initialize config
$this->config = array(
'users' => array(
array('name' => 'admin',
'password' => $password
)
),
'tokens' => array(),
'sessions' => array(),
);
that I'm saving to a file using json_encode($this->config) and later I load it using
json_decode(file_get_contents('file.json'));
it create nested objects, I would like to have this nested object when I initialize and the config, is there a way to create this nested object other then this?
$this->config = json_decode(json_encode($this->config));
array('foo' => 'bar')return{"foo": "bar"}which become an object when you usejson_decode('{"foo": "bar"}')so instead of$array['foo']you access it via$array->foo- json_decode create instance ofstdClass$this->config = new stdClass(); $admin = new stdClass(); $admin->user = 'admin'; $admin->password = $password; $this->config->users = array($admin); ...I prefer one extression. I post solution to this.Userclass and provide a constructor: you will come with something likenew User('admin', $password).