PHP:初始化关联数组时意外的T_VARIABLE


PHP: unexpected T_VARIABLE when initializing associative array

我得到以下错误解析错误:语法错误,意外T_VARIABLE 路径/queries.php第92行

因为数组$_queryArray:

private $_queryA = "";
etc...
private $_queryV = "";

private $_queryArray = array(   'A' => $this->_queryA, //<= line 92 of my code
                                'B' => $this->_queryB,
                                'C' => $this->_queryC,
                                'D' => $this->_queryD,
                                'E' => $this->_queryE,
                                'F' => $this->_queryF,
                                'G' => $this->_queryG,
                                'H' => $this->_queryH,
                                'I' => $this->_queryI,
                                'J' => $this->_queryJ,
                                'K' => $this->_queryK,
                                'L' => $this->_queryL,
                                'M' => $this->_queryM,
                                'N' => $this->_queryN,
                                'O' => $this->_queryO,
                                'P' => $this->_queryP,
                                'Q' => $this->_queryQ,
                                'R' => $this->_queryR,
                                'S' => $this->_queryS,
                                'T' => $this->_queryT,
                                'U' => $this->_queryU,
                                'V' => $this->_queryV 
                            );

我填写$_queryArray ?的方式有问题吗

由于$this引用了一个实例,并且在定义类时不存在,因此您不能在属性定义中使用$this

从文档中引用

这个声明可以包含一个初始化,但是这个初始化必须是一个常数值——也就是说,它必须能够在编译时求值,并且不能依赖于运行时信息才能求值。

我假设代码来自类声明。

我的猜测是,此时您无法访问$this。尝试在构造函数中设置数组。

function __construct() {
    $this->_queryArray = array( ... );
}