Please tell me how best to implement this. It is necessary that the properties created are static.

class Config { public function __construct() { $json = json_decode(file_get_contents('config.json'), true); foreach($json as $k=>$v){ $this->$k = $v; } } } 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

You cannot define static class properties dynamically. You can dynamically form a reference to an already existing property using self::$k and static::$k . However, it is impossible to create a new static property in the object, the static variable itself is at the class level.

I understand that you want to create a dictionary with configuration settings? Then it is better to create one static array variable and store data in it, and even better to implement the ArrayAccess interface and use the resulting object as an array (prohibiting to make changes to it).