PHP在已声明的数组中使用声明的数组变量

时间:2018-07-17 作者:Allex

我创建了一个类,在其中声明了公共关联数组。

class test{
public $basicCols= array(
      array(\'title\'=>\'KEY\', \'field\'=>\'slug\',\'options\'=>$optList),
      array(\'title\'=>\'KEY\', \'field\'=>\'slug\'),
}

public $optList= array("one"=>"One","two"=>"Two");

}
但当我执行代码时,它会给我错误。我试过了

$this->$optList
$this->optList
$optlist
是否有其他方法可以在类中的变量内声明变量。提前谢谢你。

1 个回复
最合适的回答,由SO网友:Jacob Peattie 整理而成

与WordPress相比,这更像是一个普通的PHP问题,但我建议在构造函数中设置值:

class test {
    public $basicCols;

    public $optList = array( \'one\' => \'One\', \'two\' => \'Two\' );

    function __construct() {
        $this->basicCols = array(
            array( \'title\' => \'KEY\', \'field\' => \'slug\', \'options\' => $this->optList ),
            array( \'title\' => \'KEY\', \'field\' => \'slug\' )
        );
    }
}

结束

相关推荐

Google搜索控制台:/wp-includes/php文件的索引覆盖错误

最近,在我的网站上没有任何更改,谷歌搜索控制台开始标记80个索引覆盖率错误,显示为“服务器错误(5xx)”。当我研究它时,它标记的是/wp includes/文件夹中的php文件,Google甚至不应该尝试索引这些文件。我的xml站点地图不包含这些文件。我不明白谷歌为什么要索引然后标记它们。你知道为什么会这样吗?我能做些什么来阻止它?