tags:

views:

71

answers:

3

Anybody knows the technical reason why this constraint is placed on PHP classes (at least in v5.1x)?

+3  A: 

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

troelskn
A: 

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...

Darmen
+2  A: 

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.
    }
}
Will Vousden