Php 作为onject私有属性的函数数组
我想知道这段php代码中出现“意外T_函数”错误的原因:Php 作为onject私有属性的函数数组,php,oop,anonymous-function,Php,Oop,Anonymous Function,我想知道这段php代码中出现“意外T_函数”错误的原因: class T { private $array_of_functions = array( '0' => function() { return true; } ); } 不能将此类构造用作默认属性值。默认值只能是常量表达式-因此它不能包含闭包定义,因为它是动态的(即在运行时构造时进行计算)。相反,您应该在类构造函数中初始化它: class T { private $array_of_fun
class T
{
private $array_of_functions = array(
'0' => function() { return true; }
);
}
不能将此类构造用作默认属性值。默认值只能是常量表达式-因此它不能包含闭包定义,因为它是动态的(即在运行时构造时进行计算)。相反,您应该在类构造函数中初始化它:
class T
{
private $array_of_functions = [];
public function __construct()
{
$this->array_of_functions = [
function() { return true; }
];
}
}
首先,您缺少一个分号
位于array()
定义的末尾。谢谢,但问题仍然是热门话题。应该是$this->array\u of_函数。