Home  >  Article  >  Backend Development  >  Initialization of php static variables_PHP tutorial

Initialization of php static variables_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:43:03826browse

For example:
class A {
public $f1 = 'xxxx';
static public $f2 = 100;
}


If you want to assign the variable to an object, Then it can only be initialized in the constructor, for example:
class A {
private $child;
public function __construct() {
$this->child = new B();
}
}


But there is nothing similar to the static constructor/static block in java in php, so there is no appropriate time to initialize it.


There are other solutions for shared members, for example:
class A {
static public $child;
}
A::$child = new B( );


There seems to be no clean method for private members. The only way is:
class A {
static private $child;
static public initialize( ) {
self::$child = new B();
}
}
A::initialize();

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320844.htmlTechArticleFor example: class A { public $f1 = 'xxxx'; static public $f2 = 100; } If you want to If the variable is assigned an object, it can only be initialized in the constructor, for example: class A { private $chi...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn