2

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 ?

5
  • Is $datas a static variable here? Otherwise this question makes little sense. Please provide a complete working code snippet that reproduces this error. Commented Oct 18, 2012 at 15:41
  • Usually this is worked-around with static $var = null; if (is_null($var)) $var = array( /* actual dynamic initialization */ ), which you might think could be safely implemented in PHP itself, but it isn't just because. Commented Oct 18, 2012 at 15:42
  • @deceze, yes $datas is also static Commented Oct 18, 2012 at 15:45
  • @Vivien Static variables and static properties are completely unrelated, this is a misleading reuse of a keyword in the PHP grammar. Commented Oct 18, 2012 at 15:46
  • Then you should show that properly. Commented Oct 18, 2012 at 15:46

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, never give up hope... though in this case, I think it's safe to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.