使用数组索引运算符访问类常量的子字符串

时间:2012-01-01 11:22:14

标签: php arrays string operators constants

class Foo {
 const BAR = 'Hello';
}

echo Foo::BAR; //Works
echo Foo::BAR[0]; //Parse error: syntax error, unexpected '[', expecting ',' or ';'

我已经通过使用substr找到了解决方法,但我很想知道为什么它不能正常工作。

PHP 5.3.3顺便说一下。

1 个答案:

答案 0 :(得分:3)

我想PHP认为类常量就像“经典”常量(例如用define()创建的)。

因此,它们是simply replaced by their values at runtime,因此Foo::BAR[0]将被PHP解释为'Hello'[0],这不是有效的语法($myVariable[0]被允许)。

相关问题