【问题标题】:PHP static anonymous call doesn't workPHP静态匿名调用不起​​作用
【发布时间】:2017-07-03 19:45:18
【问题描述】:

PHP 5.6、Apache 2.4 | Windows 7、OpenServer

(static function () {
    return true;
})();

为什么会引发语法错误?

syntax error, unexpected '('

但是http://php.net/manual/en/functions.anonymous.php
PS:另外->调用也不起作用...(意外的'->')

【问题讨论】:

标签: php lambda static anonymous


【解决方案1】:

这就是问题所在。不是静态部分不起作用:

$f = static function () { return true; }; $f(); //Works in PHP 5.4+

是声明和调用不起作用:

(static function () { return true; })(); //Works in PHP 7+

问题是文档说第一个语法在 PHP 5.4+ 中有效,但使用需要 PHP 7+ 才能工作的示例。

【讨论】:

    【解决方案2】:

    这在 PHP 5.x 中不起作用

    您需要 PHP 7+ 才能运行它。

    了解更多信息:https://stackoverflow.com/a/3605701/372172

    【讨论】:

    • 奇怪的是手册似乎暗示它也应该在 5.4+ 中工作
    猜你喜欢
    • 2017-05-27
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多