是否可以检查是否存在使用magic setter设置的属性?
class Test
{
private $vars;
public function __set($key, $value) {
$this->vars[$key] = $value;
}
public function &__get($key)
{
return $this->vars[$key];
}
}
$test = new Test;
$test->myvar = 'yay!';
if (magic_isset($test->myvar)) {
}
或者这不可能吗?我只需要在课堂上设置另一个函数?
使用__isset()
和isset()
:
public function __isset($key)
{
return isset($this->vars[$key]);
}
$test = new Test;
$test->myvar = 'yay!';
if (isset($test->myvar)) {
}