Check if property exists

江枫思渺然 提交于 2019-12-30 10:49:49

问题


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?


回答1:


Use __isset() and isset():

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

$test->myvar = 'yay!';

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

}


来源:https://stackoverflow.com/questions/8763357/check-if-property-exists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!