Taken from the PHP manual:
Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are no
Because an array is not a function.
While array(1,2)
looks like you are calling a function that is called array, you are doing none of the sorts. You are just making an array, which is not a function call. it is closer to saying $a = 1
.
A good question! The simple ansvar is that array()
only looks like a function, but in reality isn't one.
From the PHP documentation:
Note: array() is a language construct used to represent literal arrays, and not a regular function.
array()
is not a function, it's an initializer. Unlike ordinary functions, it's interpreted at compile time, so it can be used to initialize a static.
For the reference, this is what is allowed after the static
keyword:
static_scalar_value:
common_scalar (e.g. 42)
static_class_name_scalar (Foo::class)
namespace_name (Foo)
T_NAMESPACE T_NS_SEPARATOR namespace_name (namespace \Foo)
T_NS_SEPARATOR namespace_name (\Foo)
T_ARRAY '(' static_array_pair_list ')' e.g. array(1,2,3)
'[' static_array_pair_list ']' e.g. [1,2,3]
static_class_constant e.g. Foo::bar
T_CLASS_C (__CLASS__)
http://lxr.php.net/xref/PHP_5_5/Zend/zend_language_parser.y#945
Php 5.6 adds "static operations" to this list, which makes it possible to use expressions for statics, as long as these ultimately resolve to static scalars.
class X {
static $foo = 11 + (22/11); // syntax error in 5.5, valid in 5.6
}