Can anyone tell me why this doesn't work? It's just a crude example of what I'm trying to do somewhere else.
$stuff = array(
'key' => __DIR__ . 'value'
);
However, this produces an error:
PHP Parse error: syntax error, unexpected '.', expecting ')' in /var/www/.../testing.php on line 6
Also, this works:
$stuff = array(
'key' => "{__DIR__} value"
);
The first piece of code does not work because it's not a constant expression, as you are trying to concatenate two strings. Initial class members must be constant.
From the documentation:
[property] initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
You can't use operator in a property initialization. The workaround is to use the constructor:
public function __construct()
{
$this->stuff = array(
'key' => __DIR__ . 'value'
);
}
From the PHP doc:
This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
http://www.php.net/manual/en/language.oop5.properties.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With