3v4l.org

run code in 300+ PHP versions simultaneously
<?php class User { private $name; private $age; public function getName() { return $this->name; } public function setName($name) { if ($name != '') { // проверяем имя на непустоту $this->name = $name; } } public function getAge() { var_dump($this); return $this->$age; } private function setAge($age) { if ($age >= 0 and $age <= 70) { // проверяем возраст $this->age = $age; } } public function __set($property, $value) { $name_func = "set" . ucfirst($property); // var_dump(get_class_methods($this)); if ( in_array($name_func, get_class_methods($this)) ) { $this->$name_func($value); } } public function __get($property) { $name_func = "get" . ucfirst($property); if ( in_array($name_func, get_class_methods($this)) ) { $this->$name_func($this); } else { echo 'no'; } } } $user = new User(); $user->age = 20; echo $user->age; ?>
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
object(User)#1 (2) { ["name":"User":private]=> NULL ["age":"User":private]=> int(20) } Warning: Undefined variable $age in /in/jE9L6 on line 22 no

preferences:
86.45 ms | 402 KiB | 62 Q