Home >Backend Development >PHP Tutorial >PHP object-oriented programming and design patterns (2)_PHP tutorial
PHP Advanced Programming Study Notes 2014.06.10
This article discusses the static keyword, which can be used on variables, classes and methods.
1. Static variables
Static variables are variables that only exist in the function scope. However, after the function execution is completed, the value of such variables will not be lost. That is to say, the variables will still be remembered the next time the function is executed. original value. To define a variable as static, just add the static keyword before the variable.
<span>function</span><span> testing() { </span><span>static</span> <span>$a</span> = 1<span>; </span><span>$a</span> *= 2<span>; </span><span>echo</span> <span>$a</span>."\n"<span>; } testing(); testing(); testing(); testing(); </span><span>/*</span><span>* * 2 * 4 * 8 * 16 * [Finished in 0.1s] </span><span>*/</span>
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 the variable is 1, so 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 the static keyword in classes, one is to define static members, and the other is to define static methods. Inside a class we can use scope qualification operators to access variables at different levels of scope.
2.1. Static attributes
Static property is a class variable, which can be regarded 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.
<span>class</span><span> MyObject { </span><span>public</span> <span>static</span> <span>$a</span> = 0<span>; </span><span>function</span><span> MyMethod() { self</span>::<span>$a</span> += 2<span>; </span><span>echo</span> self::<span>$a</span> . "\n"<span>; } } </span><span>$instance1</span> = <span>new</span><span> MyObject(); </span><span>$instance1</span> -><span> MyMethod(); </span><span>$instance2</span> = <span>new</span><span> MyObject(); </span><span>$instance2</span> -><span> MyMethod(); </span><span>/*</span><span>* * * 2 * 4 * [Finished in 0.1s] * </span><span>*/</span>
$this indicator is the current instance of the class.
self:: represents the class itself. When using the self:: scope qualifier, you must add the $ symbol after the operator. This operator cannot be used in code outside the class, and it cannot Recognize your 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:: 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.
2.3. Static method
The rules for static methods are the same as static variables. Methods 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.
<span>class</span><span> MyObjectBase { </span><span>static</span> <span>function</span><span> MyMethod() { </span><span>static</span>::<span>MyOtherMethod(); } </span><span>static</span> <span>function</span><span> MyOtherMethod() { </span><span>echo</span> 'called from MyObject.'<span>; } } </span><span>class</span> MyExtendObject <span>extends</span><span> MyObjectBase { </span><span>static</span> <span>function</span><span> MyOtherMethod() { </span><span>echo</span> 'called from MyExtendObject.'<span>; } } MyExtendObject</span>::MyMethod();
The above example code will correctly call the MyOtherMethod method in MyExtendObject and output called from MyExtendObject. [Finished in 0.1s].
It is very difficult to know clearly when to use static methods and when to use non-static methods. One of the principles is that 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, delayed 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
Static variables are modified function variables. After a function is executed, its value will not be lost. Use the static keyword to create static variables and provide a default initialization value.
The static keyword can also be used in classes 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.