Home > Backend Development > PHP Tutorial > Static strain gauge php static variable initialization

Static strain gauge php static variable initialization

WBOY
Release: 2016-07-29 08:41:12
Original
1158 people have browsed it

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();
}
}
But there is nothing similar to the static constructor/static block in java in php, just There is no suitable time to initialize it.
There are 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. You can only do this:
class A {
static private $child;
static public initialize() {
self::$child = new B();
}
}
A::initialize();

The above introduces the initialization of static strain gauge PHP static variables, including the content of static strain gauge. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template