PHP 中的empty() 函数有什么神奇之处?

发布于 2024-08-24 11:53:53 字数 83 浏览 2 评论 0原文

它不应该是 __isset,因为 isset()empty() 不同

It should not be __isset,because isset() is not the same thing as empty()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄居人 2024-08-31 11:53:53

正如此页面:

__isset() 通过在无法访问时调用 isset() 或empty() 来触发
属性。

empty() 没有专用的魔术方法。

如果 __isset() 返回 true,empty() 将调用 __get() 来检查属性的值。

As it says on this page:

__isset() is triggered by calling isset() or empty() on inaccessible
properties.

There is no dedicated magic-method for empty()

If __isset() returns true, empty() will then invoke __get() to check the value of the property.

美人迟暮 2024-08-31 11:53:53

作为 Inspire 答案的补充:

class Foo {
  public function __isset($name) {
    echo "public function __isset($name)\n";
    return 'bar'===$name;
  }
  public function __get($name) {
    echo "public function __get($name)\n";
    return 'bar'===$name ? 0 : NULL;
  }
}

$foo = new Foo;
echo empty($foo->foo) ? ' empty' : ' not empty', "\n";
echo empty($foo->bar) ? ' empty' : ' not empty', "\n";

输出的

public function __isset(foo)
 empty
public function __isset(bar)
public function __get(bar)
 empty

含义是第一个属性 (foo)empty() 仅调用了返回 false 的 __isset() -> 空($foo->foo)===true
对于第二个属性 (bar),调用了 __isset() 并返回 true。然后通过 __get() 获取该属性并将其解释为布尔值(请参阅 http:// docs.php.net/language.types.type-juggling)。由于 (bool)0 为 false,empty() 也会为 empty($foo->bar) 返回 true

As an addition to Inspire's answer:

class Foo {
  public function __isset($name) {
    echo "public function __isset($name)\n";
    return 'bar'===$name;
  }
  public function __get($name) {
    echo "public function __get($name)\n";
    return 'bar'===$name ? 0 : NULL;
  }
}

$foo = new Foo;
echo empty($foo->foo) ? ' empty' : ' not empty', "\n";
echo empty($foo->bar) ? ' empty' : ' not empty', "\n";

the output is

public function __isset(foo)
 empty
public function __isset(bar)
public function __get(bar)
 empty

meaning for the first property (foo) empty() only invoked __isset() which returned false -> empty($foo->foo)===true
For the second property (bar) __isset() was invoked and it returned true. Then the property is fetched via __get() and interpreted as a boolean value (see http://docs.php.net/language.types.type-juggling). And since (bool)0 is false, empty() also returns true for empty($foo->bar)

淤浪 2024-08-31 11:53:53

我认为一般来说,您会发现以下是 PHP 的等效内容:

isset($variable[0])

例如,如果变量是字符串,则这将检测到该字符串为空。对于大多数原始类型(如果不是全部),它的工作方式类似。

I think in general you'll find that the following is the PHP equivalent:

isset($variable[0])

If, for example, the variable is a string, this would detect that the string was empty. It would work similarly for most primitive types (if not all).

多情癖 2024-08-31 11:53:53

如果您只是测试类变量是否存在,property_exists() 不适合您吗?

doesn't property_exists() work for you if you're just testing if a class variable exists?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文