Home >Backend Development >PHP Tutorial >Detailed explanation of the properties and methods of PHP static variables and the usage of delayed binding
The type keyword of static variables is static. This article is mainly about learning how to use static properties and static methods in PHP and basic examples as well as delayed binding
Static (static) keyword is used to define static methods and properties, static can also be used to define static variables and Late static binding.
1, static variable static variable
Static variables only exist in the local function scope, but when the program execution leaves this scope, its value is not lost. That is to say, the variable will still remember its original value the next time this function is executed. To define a variable as static, just add the static keyword before the variable.
function testing() { static $a = 1; $a *= 2; echo $a."\n"; } testing(); testing(); testing(); testing(); /** * 2 * 4 * 8 * 16 * [Finished in 0.1s] */
Static variables also provide a way to deal with recursive functions. A recursive function is a function that calls itself. Be careful when writing recursive functions, as they may recurse indefinitely. You must ensure that there are adequate ways to terminate recursion.
In this example, the testing() function will internally save the value of the $a variable after each execution. The next time testing() is called, the value of $a is restored, and testing() multiplies this value by 2 and prints it. The initial default value of a variable is 1, and this assignment only occurs the first time the variable is initialized. This operation will not be called during each execution of the function.
2, Use of static elements in classes
There are two main uses of static keyword in classes, one is used to define static members, the other is Kind is used to define static methods. Declaring a class property or method static allows you to access it directly without instantiating the class. Static properties cannot be accessed through an object of a class that has been instantiated (but static methods can). Static properties cannot be accessed by objects through the -> operator. Inside a class we can use scope qualification operators to access variables at different levels of scope.
2.1, Static properties
Since static methods do not need to be called through objects, the pseudo variable $this is not available in static methods. Static variables can be thought of as belonging to the entire class rather than to an instance of the class. Different from general instance variables, static properties only retain one variable value, and this variable value is valid for all instances, which means that all instances share this property.
class MyObject { public static $a = 0; function MyMethod() { self::$a += 2; echo self::$a . "\n"; } } $instance1 = new MyObject(); $instance1 -> MyMethod(); $instance2 = new MyObject(); $instance2 -> 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. When using self:: scope qualifier, you must add the $ symbol after the operator. This cannot be used in code outside the class. operator, and it does not recognize its position in the inheritance tree hierarchy. When using self:: scope in an extended class, self can call methods declared in the base class, but it will always call methods that have been overridden in the extended class.
parent:: In the extended class, when the base class method is overridden, if you want to access the base class method, you can use parent::
static:: to use We no longer need to use self:: and parent::. You can use static when you want to point to the final class that implements the functionality. This qualifier will calculate the members of the last class in the inheritance hierarchy immediately before the code is executed.
2.3, Static method
The rules for static methods are the same as static variables. A method can be marked as static using the static keyword, and static methods can be accessed through the name of the class and the scoping operator (::).
There is an important difference between static methods and non-static methods: when calling a static method, we no longer need to own 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 above example code will correctly call the MyOtherMethod method in MyExtendObject and output called from MyExtendObject. [Finished in 0.1s].
If a method does not contain the $this variable, then this method should be a static method. If you don't need an instance of a class, you should also use a static class, which eliminates the need for instantiation. In addition, the $this variable cannot be used in static methods because static methods do not belong to a specific instance.
2.4, Lazy binding
static:: eliminates the need for us to use self:: and parent:: . You can use static when you want to point to the final class that implements the functionality. This qualifier will calculate the members of the last class in the inheritance hierarchy immediately before the code is executed. This process is called delayed binding.
3, Summary
Use the static keyword to create static variables and provide a default initialization value. Static variables are modified function variables whose value remains unchanged after a function is executed.
Thestatic keyword can also be used in a class to modify properties and methods. When used on a property, it causes the property to no longer hold a value for a certain instance, but instead holds a value for the entire class itself. Static properties can be shared among members.
To access static methods, you can use (::), which is called the scope qualifier. The left side of this operator can be a class name or a predefined scope. The predefined scope includes self parent static. The right side of the operator is a static method and property.
The above is the detailed content of Detailed explanation of the properties and methods of PHP static variables and the usage of delayed binding. For more information, please follow other related articles on the PHP Chinese website!