class Foo {
const BAR = 'Hello';
}
echo Foo::BAR; //Works
echo Foo::BAR[0]; //Parse error: syntax error, unexpected '[', expecting ',' or ';'
I've found a way around this by using substr, but I'm curious to know why this doesn't work as it is.
PHP 5.3.3 by the way.
I guess that PHP considers class constants like "classic" constants (e.g. created with define()).
Therefore, they are simply replaced by their values at runtime, so Foo::BAR[0] would be interpreted by PHP as 'Hello'[0] which is not a valid syntax ($myVariable[0] being allowed).
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