StaticMembers: Members in the static class add the static modifier, which is a static member. You can directly Use class name + static member name to access this static member. Because static members exist in memory, non-static members need to be instantiated before memory is allocated, so static members cannot access non-static members. Because static members exist in memory, they are not Static members can directly access static members in the class.
1. Static global variables
Definition: Before the global variable, add the keyword static. The variable is defined as a static global variable.
Features:
A. This variable allocates memory in the global data area.
B. Initialization: If not explicitly initialized, it will be implicitly initialized to 0 (automatic variables are random unless explicitly initialized).
C. The access variable is only visible in the source file. Strictly speaking, it should start from the place of definition and end in this file.
2. Static local variables
Features:
A. This variable is in global data Allocate memory.
B. Initialization: If not explicitly initialized, it will be implicitly initialized to 0, and subsequent function calls will no longer be initialized.
C. It always resides in the global data area until the end of the program. But its scope is local scope. When the function or statement block that defines it ends, its scope ends.
Static data members follow the same public, protected, and private access rules as ordinary data members;
Because static data members allocate memory in the global data area, all objects belonging to this classShared, so it does not belong to a specific class object. Its scope is visible when no class object is generated, that is, when no instance of the class is generated, we can operate it; ''Static data member initialization and general data Member initialization is different. The format of static data member initialization is:
##Data type><Class name>::<Static data member name>=<Value>
There are two access forms for static data members of a class:cite it in the program according to the above format Static data members;
Static data members are mainly used when each object has the same Static variable initializationclass A { public $f1 = 'xxxx'; static public $f2 = 100; }
class A { private $child; public function construct() { $this->child = new B(); } }
There are ways to solve the problem for shared members, for example:
class A { static public $child; } A::$child = new B();
class A { static private $child; static public initialize() { self::$child = new B(); } } A::initialize();
The above is the detailed content of Detailed explanation of php static member variables and static variable initialization. For more information, please follow other related articles on the PHP Chinese website!