带有常量的PHP类变量

时间:2010-10-14 11:12:05

标签: php class constants class-constants

我在使用常量(DEPLOYMENT)的行上得到解析错误。为什么现在允许,或者我错过了什么。

解析错误:解析错误,期待'')''

class UploadComponent extends Object {

    private $config = array(
        'accessKey' => 'XXXX',
        'secretKey' => 'XXXX',

        'images' => array(
            'bucket' => DEPLOYMENT.'-files/images',
            'dns' => false
        ),

        'files' => array(
            'bucket' => DEPLOYMENT.'-files/files',
            'dns' => false
        ),

        'assets' => array(
            'bucket' => DEPLOYMENT.'-files/assets',
            'dns' => false
        )
    );
    ....
}

2 个答案:

答案 0 :(得分:7)

定义类变量时不能使用变量。在构造函数中初始化数组:

class UploadComponent extends Object {

    private $config;

    function __construct() {
        $this->config = array(
            'accessKey' => 'XXXX',
            'secretKey' => 'XXXX',

            'images' => array(
                'bucket' => DEPLOYMENT.'-files/images',
                'dns' => false
            ),

            'files' => array(
                'bucket' => DEPLOYMENT.'-files/files',
                'dns' => false
            ),

            'assets' => array(
                'bucket' => DEPLOYMENT.'-files/assets',
                'dns' => false
            )
        );
    }
}

答案 1 :(得分:5)

原因是'常数'可以动态定义。因此,它们的内容仅在运行时才知道,而不是编译时。