class person
{ protected $attributes = [];
public function __construct(array $attributes)
{
$this->attributes = $attributes;
}
public function __get($name)
{
return $this->attributes[$name] ?? null;
}
}
从上述代码我们可以看到 Person 对象的成员变量是通过 __get 魔术方法从 $attributes 数组中检索出来的。
class Person
{
protected $attributes = [];
public function __construct(array $attributes)
{
$this->attributes = $attributes;
}
public function __get($name)
{
return $this->attributes[$name] ?? null;
}
public function __isset($name)
{
$attribute = $this->$name;
return !empty($attribute);
}
}
class person
{ protected $attributes = [];
public function __construct(array $attributes)
{
$this->attributes = $attributes;
}
public function __get($name)
{
return $this->attributes[$name] ?? null;
}
}
从上述代码我们可以看到 Person 对象的成员变量是通过 __get 魔术方法从 $attributes 数组中检索出来的。
class Person
{
protected $attributes = [];
public function __construct(array $attributes)
{
$this->attributes = $attributes;
}
public function __get($name)
{
return $this->attributes[$name] ?? null;
}
public function __isset($name)
{
$attribute = $this->$name;
return !empty($attribute);
}
}