Here's the code that won't work :
class MyClass
{
const myconst = 'somevalue';
private $myvar = array( 0 => 'do something with '.self::myconst );
}
Seems that class constants are not available at "compile time", but only at runtime. Does anyone know any workaround ? (define won't work)
Thanks
The problem in your class declaration is not that you are using a constant, but that you are using an expression.
This simple declaration, for example, will not compile (parse error):
But if we alter your class declaration to use the simple constant, rather than a string concatenated with that constant it will work as expected.
As a work-around you could initialize your properties in the constructor:
I hope this helps you,
Alin
Note that for PHP 5.6 and above, it's possible to do so.
See https://secure.php.net/manual/en/migration56.new-features.php: