The type key for a static variable is static. This article is mainly about using methods and basic examples of static and static methods in PHP, and lazy binding
The static (static) keyword is used to define statically defined methods and properties, and static variables can also be used to define statics and late static bindings.
1 static variable variable
A static variable exists only in the local function domain, but its value is not lost when the program executes away from the scope. That is, the next time the function is executed, the variable will still remember the original value. To define a variable as static, simply precede the variable with the static keyword.
function testing () { static $a = 1; $a *= 2; echo $a. " \ n ";} Testing (); testing (); testing (); testing ();/** * 2 * 4 * 8 * * * [finished in 0.1s]*/
Static variables also provide a way to handle recursive functions. A recursive function is a function that calls itself. Be careful when writing recursive functions, because they can be recursive indefinitely. You must ensure that there is sufficient method to abort the recursion.
In this example, the testing () function saves the value of the $a variable internally after each execution. When the next testing () function is called, the value of the $a is restored, and the testing () function multiplies the value by 2 and prints. The initial default value of a variable is 1, and this assignment only occurs when the variable is first initialized. This action is not called during each execution of the function.
2. use of static elements in classes
There are two main uses of the static keyword in a class, one for defining static members, and the other for defining static methods. Declaring a class property or method as static can be accessed directly without instantiating the class. Static properties cannot be accessed through an object that a class has instantiated (but static methods can). A static property cannot be accessed by an object through the operator. Inside a class, we can use scope-qualified operators to access variables of different levels of scope.
2.1. Static Properties
Because static methods do not need to be called through an object, the pseudo-variable $this is not available in a static method. You can think of static variables as belonging to an entire class and not to an instance of a class. Unlike a typical instance variable, a static property retains only one variable value, which is valid for all instances, meaning that all instances share this property.
Class myobject{public static $a = 0; function MyMethod () {self :: $a + = 2; echo Self:: $a. "\ n"; }} $instance 1 = new MyObject (), $instance 1, MyMethod (), $instance 2 = new MyObject (); $instance 2, MyMethod ();/** * * 2 * 4 * [finished in 0.1s] * * *
$this indicator is the current instance of the class and is a reference to the calling object.
Self:: Represents the class itself, using the self:: The scope qualifier must be followed by the $ symbol after the operator, which cannot be used in code outside the class, and it does not recognize its position in the inheritance tree hierarchy. When you use the self:: scope in an extension class, self can call a method declared in the base class, but it always calls a method that has been overridden in the extension class.
Parent:: In an extension class, if the method of the base class is overridden, you can use the parent if you want to access the method of the base class:
Static:: So that we no longer need to use Self:: and Parent::. When you want to point to a class that ultimately implements functionality, you can use static, which immediately calculates the members of the last class on the inheritance hierarchy before the code executes.
2.3. static method
The rules for static methods are the same as static variables. You can use the Static keyword to mark a method as a static method, whereas a static method can be accessed by the name and scope of the class (::).
There is one important difference between static and non-static methods: When you call a static method, we do not need to have an instance of the class.
Class myobjectbase{ static function MyMethod () { static::myothermethod (); } static function Myothermethod () { echo ' called from MyObject. '; }} Class Myextendobject extends myobjectbase{ static function Myothermethod () { echo ' called from Myextendobject. '; }} Myextendobject::mymethod ();
The previous example code correctly calls the Myothermethod method in Myextendobject, outputting called from Myextendobject. [Finished in 0.1s].
If a method does not contain a $this variable, then this method should be a static method. If you do not need an instance of the class, you should also use a static class, which avoids the instantiation of the work. In addition, $this variables cannot be used in static methods, because static methods do not belong to a particular instance.
2.4. Delay Binding
Static:: So that we no longer need to use Self:: and Parent::. When you want to point to a class that ultimately implements functionality, you can use static, which immediately calculates the members of the last class on the inheritance hierarchy before the code executes. This process is called deferred binding.
3. Summary
You can use the static keyword to create a static variable, and you can also provide a default initialization value. A static variable is a modified function variable whose value remains intact after a function has been executed.
The static keyword can also be used in classes to decorate properties and methods. When used on a property, it causes the property to no longer hold a value for an instance, but instead holds a value for the entire class itself, which can be shared among members.
To access a static method can be used (::), which is called the scope qualifier. The left side of this operator can be a class name or a predefined scope, including the self parent static. The right side of the operator is a static method, property.