开发者

PHP Class variables with Constants

开发者 https://www.devze.com 2023-01-20 03:16 出处:网络
I am getting a parse er开发者_开发百科ror on the lines with the constant (DEPLOYMENT). Why is this now allowed, or am I missing something.

I am getting a parse er开发者_开发百科ror on the lines with the constant (DEPLOYMENT). Why is this now allowed, or am I missing something.

Parse error: parse error, expecting `')'' in

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
        )
    );
    ....
}


You can't use variables when defining class vars. Initialize your array inside the constructor instead:

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
            )
        );
    }
}


The reason is that 'constants' can be defined dynamically. Their contents are therefore only known at run-time, and not compile-time.

0

精彩评论

暂无评论...
验证码 换一张
取 消