Static variables within functions
Understanding of static static variables
Static variables The type specifier is static.
Static variables belong to the static storage method, and their storage space is the static data area in the memory (storage units are allocated in the static storage area). The data in this area occupies these storage spaces throughout the running of the program. (It is not released during the entire running of the program), and it can also be considered that its memory address remains unchanged until the end of the entire program (on the contrary, auto automatic variables, that is, dynamic local variables, belong to the dynamic storage category and occupy dynamic storage space. Functions Released after the call is completed). Although static variables always exist throughout the execution of the program, they cannot be used outside its scope.
In addition, quantities belonging to static storage methods are not necessarily static variables. For example: Although external variables belong to the static storage method, they are not necessarily static variables. They must be defined by static before they can become static external variables, or static global variables.
All global variables are static variables, and local variables are local static variables only when they are defined with the type modifier static.
Static variables can be applied anywhere. Once the application is successful, it will no longer accept other similar applications.
Static variables do not mean that they cannot change the value. A quantity that cannot change the value is called a constant. The value it holds is mutable, and it will remain up-to-date. It is said to be static because it does not change when the function is called or exits. That is, if we assign a certain value to a static variable the last time the function is called, the value will remain unchanged the next time the function is called.
Static variables within the function
static usage
1, please see the following example:
function doStuff(&$cache) { static $cache = null; if ($cache === null) { echo $cache = '%heavy database stuff or something%'; } } $cache = 'not null'; doStuff($cache); // Output %heavy database stuff or something%
As can be seen from the above example, the static keyword affects reference transfer. Even if we use & to try to change the value and address of the variable $cache, it still does not affect the if judgment in the doStuff() function;
Moreover, in the doStuff() function, the static variable $cache is not immutable. $cache changes from null to %heavy database stuff or something%;
Static methods in the class and Properties
● We regard classes as templates for generating objects, and objects as active components. We instantiate a class, get an object, and then access the methods and properties of this object.
For example $foo = new Foo(); $foo is the object after instantiation of class Foo.
● Static methods are functions with class as scope. We can directly access static methods without instantiation.
For example:
class Foo() { public static function a(){} } // 访问a(); Foo::a();
● Static methods cannot be accessed Ordinary properties and methods in this class, because those properties and methods belong to an object, while static methods and properties are also called methods of class variables.
● To access static methods or properties in the current class (non-subclass) use self::method(), note: self can call the static methods and properties of the parent class;
Delayed static binding
Let’s look at an example first
header("Content-type: text/html; charset=utf-8"); class A { public static function aa() { echo "非延迟静态绑定<br>"; } public static function bb() { echo self::aa(); // Output 非延迟静态绑定 echo static::aa(); // Output 延迟静态绑定 } } class B extends A { public static function aa() { echo "延迟静态绑定"; } public static function cc() { echo self::bb(); } } B::bb(); // Output 非延迟静态绑定 延迟静态绑定
Using the self keyword refers to the current class (A), so what is obtained is the aa() method of class A The return value;
After php5.3, we can use static to get the aa() method of the subclass, which refers to the called class.
Summary:
The delayed binding of the static keyword has many uses, and it can generally be summarized while doing it in the project.
The above is the detailed content of Understanding the static keyword in php. For more information, please follow other related articles on the PHP Chinese website!