PHP member variables can be initialized at the same time as they are declared, but they can only be initialized with scalars, for example:
class A { public $f1 = 'xxxx'; static public $f2 = 100; } |
If you want to assign a variable to an object, it can only be initialized in the constructor, for example:
class A { private $child; public function __construct() { $this->child = new B(); } } |
class A {
} A::$child = new B(); |
There seems to be no clean way for private members, the only way is to do this:
class A { static private $child; static public initialize() { self::$child = new B(); } } A::initialize(); |
class A { static private $child; static public initialize() { |