static (static) keywords are used to define statics and properties and static can also be used to define static variables and later static bindings.
1. static variable variable
A static variable exists only in a local function field, but its value is not lost when the program executes out of this 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.
Copy Code code as follows:
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 handle recursive functions. A recursive function is a function that calls itself. Be careful when writing recursive functions, as you may be able to recursively go down indefinitely. There is a need to ensure that there are adequate methods to abort 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 then the testing () function multiplies the value by 2 and prints it. The initial default value of a variable is 1, which occurs only when the variable is first initialized. This action is not invoked during each execution of the function.
2, the use of static elements in the class
There are two main uses of the static keyword in a class, one for defining static members and the other for defining static methods. Declares that a class property or method is static and can be accessed directly without instantiating the class. Static properties cannot be accessed through an object that is instantiated by a class (but static methods can). Static properties cannot be accessed by an object through the-> operator. Within the class we can access variables of different levels of scope using scope-qualified operators.
2.1. Static Properties
Because a static method does not need to be invoked through an object, the pseudo variable $this not available in the static method. Static variables can be viewed as belonging to an entire class rather than to an instance of a class. Unlike a generic instance variable, a static property retains only one variable value, which is valid for all instances, meaning that all instances share this property.
Copy Code code as follows:
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]
*
*/
The $this metric is the current instance of the class and is a reference to the main object.
Self:: Represents the class itself, using self:: The scope qualifier must be preceded by a $ symbol followed by the operator, which cannot be used in code other than the class, and it does not recognize its position in the inheritance tree hierarchy. When you use Self:: scope, Self can call a method declared in the base class, but it calls a method that is always overridden in an extended class.
Parent:: In the extended class, if the method of the base class is overridden, if you want to access the method of the base class, you can use Parent::
Static:: So that we no longer need to use Self:: and Parent::. You can use static when you want to point to a class that ultimately implements the functionality, which calculates the members of the last class on the inheritance hierarchy immediately 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, while using the class's name and scope-qualified operators (::) can access static methods.
There is an important difference between static and Non-static methods: When calling a static method, we do not need to have an instance of the class.
Copy Code code as follows:
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 correctly invokes 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 a class, you should also use a static class, which eliminates 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::. You can use static when you want to point to a class that ultimately implements the functionality, which calculates the members of the last class on the inheritance hierarchy immediately 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 decorated function variable whose value remains unchanged after a function has been executed.
The static keyword can also be used in a class to decorate properties and methods. When used on a property, it causes a property to no longer hold a value for an instance, but instead saves a value for the entire class itself, which can be shared among members.
To access a static method, it can be used (::), which is called the scoping qualifier. The left side of this operator can be a class name or a predefined scope, and the predefined scopes include self parent static. The right side of the operator is a static method, a property.