当给定非变量时,为什么空需要 T_PAAMAYIM_NEKUDOTAYIM ?

发布于 2024-10-19 21:50:25 字数 377 浏览 3 评论 0原文

<?php
define('foo', 'bar');

if (empty(foo)) {
  echo 'qux';
}

http://codepad.org/G1TSK1c6
解析错误:语法错误,意外的 ')',期望在第 4 行出现 T_PAAMAYIM_NEKUDOTAYIM

我知道 empty() 只允许变量作为参数传递,但为什么它期望 T_PAAMAYIM_NEKUDOTAYIM (即 ::) 当我给它一个常量时?

<?php
define('foo', 'bar');

if (empty(foo)) {
  echo 'qux';
}

http://codepad.org/G1TSK1c6
Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM on line 4

I know that empty() only allows variables to be passed as an argument, but why does it expect a T_PAAMAYIM_NEKUDOTAYIM (i.e. ::) when I give it a constant?

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

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

发布评论

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

评论(5

倾听心声的旋律 2024-10-26 21:50:25

解析器想要的下一个合乎逻辑的东西是 :: 因为 foo 不是变量。

if (empty(foo::$bar)) {
}

这是唯一在 empty() 未传递变量时起作用的东西。您的示例被评估为 empty(bar),其中解析器假定 bar 是类名,并且现在需要一个静态成员变量。

The next logical thing the parser wants is a :: because foo is not a variable.

if (empty(foo::$bar)) {
}

Is the only thing, that works when empty() is not passed a variable. Your example is evaluated as empty(bar) where the parser assumes bar to be a class name and now expects a static member variable.

寻梦旅人 2024-10-26 21:50:25

在做一些研究时看到了这个,虽然我知道这是一个障碍,但我想我会更好地澄清这一点。

empty() 不是一个函数,而是一个语言构造。这意味着底层解析器代码与解析发送到常规函数或方法的参数的代码不同。当您遇到类似的服务错误消息时,一开始这可能看起来不一致,但是让我们稍微分解一下。

empty() 期望检查变量;常量不是变量,因此它不包含在可以传递给此构造的可能语法列表中,这非常有意义,因为否则您会做一些不合逻辑的事情(检查常量值中是否为空)。

我们在 PHP 中唯一可能的其他变量是好的旧变量和类属性,因为它们是可以互换的,相关语法可以如下表示:

<name>     ::= [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
<variable> ::= $ <name>
<property> ::= <name> :: <name> || $ <name> :: <name> || $ <name> -\> <name>

这意味着如果您传递一个常量,PHP 会将其读取为类名,因此需要双冒号标记 '::',因此该错误非常有意义。

希望这有点有见地;-)

Saw this one while doing some research, although I know this is a bump I thought I would clarify this better.

empty() is not a function, but a language construct. This means that the underlaying parser code is different from that of parsing arguments sent to regular functions or methods. This may seem inconsistent at first when you experience serve error messages like that one, however lets break it down a little.

empty() is expecting to check something thats variable; a constant is not variable so that is not included in the list of possible syntaxes that can be passed to this construct, which makes perfect sense as you would otherwise be doing something illogical (checking for emptiness in a constant value).

The only other possible variables we have in PHP is good old variables and class properties as they are interchangeable, the relevant syntaxes can be represented like this:

<name>     ::= [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
<variable> ::= $ <name>
<property> ::= <name> :: <name> || $ <name> :: <name> || $ <name> -\> <name>

This means that if you are passing a constant, PHP will read this as the class name and therefore is expecting the double colon token '::', hence why the error makes perfect sense.

Hope this was somewhat insightful ;-)

黑色毁心梦 2024-10-26 21:50:25

我对此进行了研究,并在本地 PHP 安装上进行了尝试,你猜怎么着?它运行顺利(PHP 5.5.6)。在不同版本的 PHP 上尝试相同的代码后,我发现它并不适用于所有版本的 PHP < 5.5.x 并以其他方式工作。

然后我查看了 PHP 的文档,更具体地说是它从 5.4.x 到 5.5.x 的变更日志,并发现了这个:

http://www.php.net/manual/en/migration55.new-features.php#migration55.new-features.empty

empty()支持任意表达式

现在支持将任意表达式而不是变量传递给empty()。例如:

if (empty(TRUE)) {
    echo "This will NOT be printed.\n";
}

if (empty(FALSE)) {
    echo "This will be printed.\n";
}

上面的例子将输出:

这将被打印。

因此,如果您运行 PHP >= 5.5.x,那么这将不是问题。

您可以使用此服务在不同版本的 PHP 上测试代码:http://sandbox.onlinephpfunctions.com/ 。我一生都无法弄清楚如何保存代码示例(我总是无法通过验证码,即使它非常简单 - 我认为有些东西可能最终会被破坏)。

I looked into this, tried it on my local PHP installation and guess what? It worked without a hitch (PHP 5.5.6). After trying the same code on different versions of PHP, I found that it does not work on all versions of PHP < 5.5.x and works otherwise.

So then I took to PHP's documentation, more specifically its changelog from 5.4.x to 5.5.x, and found this:

http://www.php.net/manual/en/migration55.new-features.php#migration55.new-features.empty

empty() supports arbitrary expressions

Passing an arbitrary expression instead of a variable to empty() is now supported. For example:

if (empty(TRUE)) {
    echo "This will NOT be printed.\n";
}

if (empty(FALSE)) {
    echo "This will be printed.\n";
}

The above example will output:

This will be printed.

So if you're running PHP >= 5.5.x, then this will not be a problem.

You can test the code on different versions of PHP using this service: http://sandbox.onlinephpfunctions.com/. I could not, for the life of me, figure out how to save code samples (I always failed the captcha, even though it's dead simple - I think something MAY be broken on their end).

红颜悴 2024-10-26 21:50:25

empty() 需要变量而不是常量。您应该使用define()作为常量。

empty() expects variables and not constants. You should use defined() for constants.

落叶缤纷 2024-10-26 21:50:25

只是一个,但我认为解析代码时会抛出此错误。

foo,不是变量,也不是字符串,因此,在解析的上下文中,下一个解决方案可能是class属性,但并不是因为没有::,但这应该是因为这里不应该使用常量,它仍然是类属性或方法。

Just a though, but I think this error is thrown when parsing the code.

foo, is not a variable, nor a string, therefore, in the context of parsing, the next solution may be class attribute., but it is not because there is no ::, but it should be because constants should not be used here, and it remains to be a class attribute or method.

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