为何 php empty 函数判断结果为空,但实际值却为非空

本文首发于 震惊 php empty 函数判断结果为空,但实际值却为非空,转载请注明出处。

最近我在一个项目中使用 empty 时获取到了一些意料以外的结果。下面是我处理后的调试记录,在这里与你分享了。php

var_dump(
    $person->firstName,
    empty($person->firstName)
);

它的结果是:数组

string(5) "Freek"
bool(true)

结果出人意料。为何变量的值为字符串,但同时会是空值呢?让咱们在 $person->firstName 变量上尝试使用其它一些函数来进行判断吧:数据结构

var_dump(
    $person->firstName,
    empty($person->firstName),
    isset($person->firstName),
    is_null($person->firstName)
);

以上结果为:函数

string(5) "Freek"
bool(true) // empty
bool(true) // isset
bool(false) // is_null
译者注:这边的结果可能存在问题 isset 的结果一样为 false,能够到 这里 去运行下查看结果。

issetis_null 函数执行结果符合预期判断,惟独 empty 函数返回了错误结果。this

这里让咱们来看看 person 类的实现代码吧:调试

class person
{
    protected $attributes = [];

    public function __construct(array $attributes)
    {
        $this->attributes = $attributes;
    }

    public function __get($name)
    {
        return $this->attributes[$name] ?? null;
    }
}

从上述代码咱们能够看到 Person 对象的成员变量是经过 __get 魔术方法从 $attributes 数组中检索出来的。code

当将变量传入一个普通函数时,$person->firstName 会先进行取值处理,而后再将获取到的结果做为参数传入函数内。对象

可是 empty 不是一个函数,而是一种数据结构。因此当将 $person->firstName** 传入 **empty** 时,并不会先进行取值处理。而是会先判断 **$person 对象成员变量 firstName 的内容,因为这个变量并未真实存在,因此返回 falseblog

在正中应用场景下,若是你但愿 empty 函数可以正常处理变量,咱们须要在类中实现 __isset 魔术方法。字符串

class Person
{
    protected $attributes = [];

    public function __construct(array $attributes)
    {
        $this->attributes = $attributes;
    }

    public function __get($name)
    {
        return $this->attributes[$name] ?? null;
    }

    public function __isset($name)
    {
        $attribute = $this->$name;

        return !empty($attribute);
    }
}

这是当 empty 进行控制判断时,会使用这个魔术方法来判断最终的结果。

再让咱们看看输出结果:

var_dump(
   $person->firstName, 
   empty($person->firstName)
);

新的检测结果:

string(5) "Freek"
bool(false)

完美!

原文:When empty is not empty

相关文章
相关标签/搜索