检查属性是否存在

检查属性是否存在

问题描述:

是否可以检查是否存在使用魔术师设置的属性?

Is it possible to check if a property exists which are set using 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)) {
}

还是不可能,我只需要在课堂上设置另一个功能?

Or isn't it possible and I just need to setup another function in my class?

使用 __isset() isset() :

public function __isset($key)
{
    return isset($this->vars[$key]);
}

$test = new Test;

$test->myvar = 'yay!';

if (isset($test->myvar)) {

}