dongping4901 2010-09-17 12:29
浏览 6
已采纳

Php Magic方法和空

Having the following code

class test {
    private $name;
    public function __get($name){
        return $name;
    }
    public function __set($name,$value){
        $this->name = $value;
    }
}
$obj = new test();
$obj->a = 2;

if (!empty($obj->a)) {
    echo 'not empty';
}

This is calling __isset. But this is not being defined so it always return empty. What is the best way to check for a non empty property?

Update :changing the class is not a solution because it's a 3th party component and it has to remain intact.

  • 写回答

4条回答 默认 最新

  • dsdqpdjpq16640651 2010-09-17 13:41
    关注

    If you can't change the class, I think the only possible workaround is using a temporary variable.

    $obj->a = 2;
    
    $test = $obj->a; 
    
    if (!empty($test)) {
        echo 'not empty';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?