31

can you initialize a static array of objects in a class in PHP? Like you can do

class myclass {
    public static $blah = array("test1", "test2", "test3");
}

but when I do

class myclass {
    public static $blah2 = array(
        &new myotherclass(),
        &new myotherclass(),
        &new myotherclass()
    );
}

where myotherclass is defined right above myclass. That throws an error however; is there a way to achieve it?

5
  • Could you tell us that the error is? Commented May 27, 2012 at 3:52
  • 5
    Set $blah2 within the constructor. You can't set runtime-calculated values in a property definition. Commented May 27, 2012 at 3:56
  • @Wiseguy did I get your message right? Commented May 27, 2012 at 4:10
  • @wiseguy: OP wants a static array - one per class. Initializing it every time you create a new instance seems like a bad approach.. Commented May 27, 2012 at 4:10
  • @user1181950: on an unrelated note, using new by reference there has been depreciated for several years now. Maybe it's slipped under PHP's radar and that's why it still remains to this day without causing a fatal error. But, just so you know. Commented May 27, 2012 at 4:16

3 Answers 3

32

Nope. From http://php.net/manual/en/language.oop5.static.php:

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.

I would initialize the property to null, make it private with an accessor method, and have the accessor do the "real" initialization the first time it's called. Here's an example:

    class myclass {

        private static $blah2 = null;

        public static function blah2() {
            if (self::$blah2 == null) {
               self::$blah2 = array( new myotherclass(),
                 new myotherclass(),
                 new myotherclass());
            }
            return self::$blah2;
        }
    }

    print_r(myclass::blah2());
Sign up to request clarification or add additional context in comments.

Comments

3

While you cannot initialize it to have these values, you can call a static method to push them into its own internal collection, as I've done below. This may be as close as you'll get.

class foo {
  public $bar = "fizzbuzz";
}

class myClass {
  static public $array = array();
  static public function init() {
    while ( count( self::$array ) < 3 )
      array_push( self::$array, new foo() );
  }
}

myClass::init();
print_r( myClass::$array );

Demo: http://codepad.org/InTPdUCT

Which results in the following output:

Array
(
  [0] => foo Object
    (
      [bar] => fizzbuzz
    )
  [1] => foo Object
    (
      [bar] => fizzbuzz
    )
  [2] => foo Object
    (
      [bar] => fizzbuzz
    )
)

Comments

-1

I do it like this. It’s not “static” but the result is the same on construct.

class Foo {
  var $bar= [];
  function __construct(){
    $this->bar[] = (object)array( 
      “name” => “Harry”,
      “hair” => “Blonde”
    );    
    $this->bar[] = (object)array( 
      “name” => “Jane”,
      “hair” => “Black”
    );
  }

$foo = new foo;
print_r($foo->bar);
Array (
[0] => stdClass Object
    (
        [name] => Harry
        [hair] => blonde
    )

[1] => stdClass Object
    (
        [name] => Jane
        [hair] => black
    )

)

Comments

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.