开发者

What's the technical reason why class constants can't be arrays in PHP?

Anybody knows the technical reason why this constraint is pla开发者_如何转开发ced on PHP classes (at least in v5.1x)?


Arrays are variable - you can modify them. You can use a static property instead.


Constants cannot contain mutable types. A constant is a "variable" that cannot be changed; it cannot be assigned to, but if its value were mutable, then it could be changed just by mutating the value:

class SomeClass
{
    public const $array = array(0 => 'foo', 1 => 'bar');

    public static function someFunction()
    {
        self::$array[0] = 'baz'; // SomeClass::$array has now changed.
    }
}


Don't exactly know why, but you can initialize static array variable:

class myClass {
   public static $arr = Array ('foo', 'bar'); 
}

Note that arrays are variables, so you can modify them outside...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜