PHP Class variables with Constants
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.
精彩评论