Php 带有运行时函数的公共/私有变量声明

Php 带有运行时函数的公共/私有变量声明,php,Php,我试图在类中设置一个公共/私有(无所谓)变量(数组) 以这种方式(非常精简) 这给了我一个错误 分析错误:语法错误,意外“.”,应为“.” 为什么??我一直这样声明数组,而且没有问题 解决方案:首先在问题下方发表评论。请尝试: class Test extends Whatever { private $rules; public function __construct() { $this->rules = array( 'f

我试图在类中设置一个
公共/私有
(无所谓)变量(数组)

以这种方式(非常精简)

这给了我一个错误

分析错误:语法错误,意外“.”,应为“.”

为什么??我一直这样声明数组,而且没有问题


解决方案:首先在问题下方发表评论。

请尝试:

class Test extends Whatever {

    private $rules;

    public function __construct() {
        $this->rules = array(
            'folder' => 'files/game/pictures/' . date('Ymd'), //this line causes error mentioned below
        );
    }

}

类声明的变量未使用数组(它是一个对象)。 试试这个:

class Test extends Whatever {

    private $rules = array();

    public function __construct() {
        $this->rules = array('folder' => 'files/game/pictures/' . date('Ymd'));
        // Other code...
    }
}

声明可能包括一个初始化,但该初始化必须是一个常量值——也就是说,它必须能够在编译时进行计算,并且不能依赖于运行时信息才能进行计算-这包括函数调用的结果。谢谢,我在一个多小时内一直在寻找这个文档。当我看到1号时,我知道答案,但是谢谢。当我看到1号时,我知道答案,但是谢谢。
class Test extends Whatever {

    private $rules = array();

    public function __construct() {
        $this->rules = array('folder' => 'files/game/pictures/' . date('Ymd'));
        // Other code...
    }
}