- Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
- int(20)
<?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->age);
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;
?>