0

Would it be possible to initialize a protected SplObjectStorage as a map within a class? I seem to be running into an error whenever I try this. Similar to example below:

class a {

  protected $a = new SplObjectStorage();

  ...

}
halfer
  • 19,824
  • 17
  • 99
  • 186
snehoozle
  • 273
  • 2
  • 4
  • 14
  • 1
    You can only initialise a class property with a simple type or an array. It would be great to be able to do what you want, but this is another one of PHP's artificial limitations – Bojangles Jun 08 '14 at 22:09
  • 2
    You will have to initialize that inside the constructor. – Jon Jun 08 '14 at 22:09
  • You can't do this, but you may wish to consider injecting the inner instance into the outer one, so that it is not hard-wired. – halfer Jun 08 '14 at 22:48

1 Answers1

0

you need to use constructor

class a {
  public function __construct() {
     $this->a = new SplObjectStorage();
  }

  protected $a;

  ...
}
Alon Eitan
  • 11,997
  • 8
  • 49
  • 58