Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 作为onject私有属性的函数数组_Php_Oop_Anonymous Function - Fatal编程技术网

Php 作为onject私有属性的函数数组

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

我想知道这段php代码中出现“意外T_函数”错误的原因:

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_函数。