PHP类中的函数字面量 [英] Function literal in PHP class
问题描述
请看这段代码,请:
$ array = array(
'action'=> ; function(){echothis works;}
);
$ b $ class Test {
public $ array = array(
action=> function(){echothis does not;}
);
}
第一个函数文字解析得很好,但第二个 - - 触发语法错误:
$ b
解析错误:语法错误,意外的'function'(T_FUNCTION)...
有人可以向我解释这个吗?这是一个错误?
编辑:这是最新的PHP:5.6.6
$ b $ b声明可能包含一个初始化,但是这个
初始化必须是一个常量值 - 也就是说,它必须能够
被评估 strong>在编译时,并且必须不依赖于运行时
信息才能被评估。
http://php.net/ manual / en / language.oop5.properties.php
Take a look at this code, please:
$array = array(
'action' => function () { echo "this works"; }
);
class Test {
public $array = array(
"action" => function () { echo "this doesn't"; }
);
}
The first function literal parses fine, but the second - the one inside the class - triggers a syntax error:
Parse error: syntax error, unexpected 'function' (T_FUNCTION)...
Can somebody explain this to me? Is this a bug?
EDIT: This is the latest PHP: 5.6.6
From the class it's a property !
Rule from properties :
Declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
http://php.net/manual/en/language.oop5.properties.php
这篇关于PHP类中的函数字面量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!