7

I'm trying to declare a public static variable that is a array of arrays:

class Foo{
 public static $contexts = array(
    'a' => array(
      'aa'              => something('aa'),
      'bb'              => something('bb'),
    ),

    'b' => array(
      'aa'              => something('aa'),
      'bb'              => something('bb'),
    ),

  );

 // methods here

 }

 function something($s){
   return ...
 }

But I get a error:

Parse error: parse error, expecting `')'' in ...

5
  • What is something()? Also, is this declared as a class property (public static $contexts) or somewhere in a method? Commented Jun 28, 2011 at 1:45
  • it's a normal function.. it's declared outside the class. the variable is declared as a class property Commented Jun 28, 2011 at 1:46
  • "declared outside the class"? Can we see where this bit of code is with the rest of the class? Commented Jun 28, 2011 at 1:48
  • "Outside." "Class property." Which is it? Please show more complete code. Commented Jun 28, 2011 at 1:48
  • I mean the function is declared outside, the variable is public static ... inside the class. I edited my q to make it more clear Commented Jun 28, 2011 at 1:49

1 Answer 1

13

You can't use expressions when declaring class properties. I.e. you can't call something() here, you can only use static values. You'll have to set those values differently in code at some point.

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.

http://www.php.net/manual/en/language.oop5.static.php

For example:

class Foo {
    public static $bar = null;

    public static function init() {
       self::$bar = array(...);
    }
}

Foo::init();

Or do it in __construct if you're going to instantiate the class.

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

2 Comments

that's weird, because I can just declare a public static function that will return my array, it would be the same
The initial values of class properties are created while the source code is parsed. At that point memory needs to be reserved for those initial class values since they need to be stored somewhere. This happens before the code is actually executed. You can't reserve memory for the return value of a function though, since a function may return anything. And since parsing hasn't completed, functions can't be executed yet. Hence, while parsing the code, only static values of known size are allowed. A function is (explicitly) invoked later at runtime and may return anything.

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.