empty() 在对象的非空属性上返回 TRUE [英] empty() returning TRUE on object's non-empty property

查看:50
本文介绍了empty() 在对象的非空属性上返回 TRUE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常奇怪和意想不到的问题.

I've got a very weird and unexpected problem.

empty() 在非空属性上返回 TRUE ,原因我不知道.

empty() is returning TRUE on a non-empty property for a reason unknown to me.

class MyObject
{
    private $_property;

    public function __construct($property)
    {
        $this->_property = $property;
    }

    public function __get($name)
    {
        $priv_name = "_{$name}";

        if (isset($this->$priv_name))
        {
            return $this->$priv_name;
        }
        else
        {
            return NULL;
        }
    }
}

$obj = new MyObject('string value');

echo $obj->property;        // Output 'string value'
echo empty($obj->property); // Output 1 (means, that property is empty)

这是否意味着在使用 empty() 时不会调用 __get() 魔法函数?

Would this mean, that the __get() magic function is not called when using empty()?

顺便说一句.我正在运行 PHP 5.0.4 版

btw. I'm running PHP version 5.0.4

推荐答案

是的,就是这个意思.empty 不是你的日常功能,它是一种不符合正常规则的语言结构.因为实际上$obj->property是不存在的,所以结果是正确的.

Yes, that's what it means. empty is not your everyday function, it's a language construct that doesn't play by normal rules. Because in fact, $obj->property does not exist, so the result is correct.

您需要实现__isset() 使 emptyisset 工作.

这篇关于empty() 在对象的非空属性上返回 TRUE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆