Александр Антонов, а где у вас там типы? Один массив и строки я увидел, ну и сам json это строки по сути, можно магии повесить, ваш json после decode массив, его кидаете в конструктор, тупо ключ => значение
Далее записываете его в свойство, а из свойства магический геттер
class MyJson
{
private array $data;
public function __construct(array $array)
{
$this->data = $array;
}
public function __call(string $name, array $arguments)
{
$action = substr($name, 0, 3);
$property = strtolower(substr($name, 3));
switch ($action) {
case 'get':
return $this->data[$property ] ?? null;
case 'set':
$this->data[$property] = $arguments[0];
break;
}
}
public function __set(string $name, $value)
{
$this->data[$name] = $value;
}
public function __get(string $name)
{
return array_key_exists($name, $this->data)) ? $this->data[$name] : null;
}
public function __isset($name): bool
{
return isset($this->data[$name]);
}
public function __unset($name): void
{
unset($this->data[$name]);
}
}
$json = new MyJson(json_decode($json, true));
echo $json->name;
$json->name = 'test';
echo $json->getName();
$json->setName('test2');
echo $json->name;
Писал с телефона. Могут быть опечатки.
Почитать тут
https://www.php.net/manual/ru/language.oop5.magic.php