Initialization of PHP static variables

  • 2020-03-31 16:42:59
  • OfStack

Such as:
Class A {
Public $f1 = 'XXXX';
Static public $f2 = 100;
}


If you want to assign a variable to an object, you can only initialize it in the constructor, for example:
Class A {
Private $child;
The public function __construct () {
$this - > The child = new B ();
}
}


But PHP doesn't have anything like the static constructor/static block in Java, so there's no good time to initialize it.


There are also solutions for common members, such as:
Class A {
The static public $child;
}
A: : $child = new B ();


There seems to be no clean way for private members to do this:
Class A {
The static private $child;
The static public the initialize () {
The self: : $child = new B ();
}
}
A: : initialize ();

Related articles: