Check if property exists

前端 未结 1 1642
被撕碎了的回忆
被撕碎了的回忆 2021-01-14 11:33

Is it possible to check if a property exists which are set using magic setter?

class Test
{
    private $vars;

    public function __set($key, $value) {
            


        
1条回答
  •  悲&欢浪女
    2021-01-14 12:15

    Use __isset() and isset():

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

    0 讨论(0)
提交回复
热议问题