Vivien
Vivien

Reputation: 1169

Static variable in static array

I'm trying to insert a static variable in an array like this :

static $datas = array(
    'link' => config::$link
);

But i'm having this error

Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING

I discovered PHP doc say that :

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

But I'm sure there is a way to do that, any suggestion ?

Upvotes: 2

Views: 2351

Answers (1)

deceze
deceze

Reputation: 522165

No, there is no workaround. static variables and properties can only be initialized with constant values. That means literals or constants. Variables, static or not, cannot be used, period. You have to assign a variable value later using procedural code somewhere.

Upvotes: 5

Related Questions