This article introduces the static keyword in PHP as well as the difference with the Self keyword, this article explains the definition of the static keyword, late binding (Late static bindings), and the difference with the Self keyword, and so on, you need friends can refer to the
Overview
Learning design patterns, before an article on the single case mode of the article, reread this article, found that the static keyword master is not very secure, to review.
Static keyword
The static keyword is described in the PHP manual as follows:
The code is as follows:
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot is accessed with the instantiated class object (though a static method can).
The general meaning is that after you declare the properties and methods of a class as static, you can access static properties and methods directly without instantiating the object.
The characteristics of static members and methods in PHP are as follows:
1. Static members cannot be accessed through instances of the class, but static methods can.
2. Static members cannot be accessed through the-> operator.
3. In the scope of a static method, the $this keyword cannot appear, meaning that ordinary member variables cannot be accessed in a static method.
4. Static members and methods can all be accessed directly through the class name without instantiating the object.
Late binding (Late Static bindings)
The following is excerpted from the PHP manual:
The code is as follows:
Since PHP 5.3.0, PHP has added a feature called late static binding, which is used to reference statically invoked classes within the inheritance scope.
To be exact, late-stage static binding works by storing the class name of the last non-forwarding call. When a static method call is made, the class name is the one that is explicitly specified (usually on the left side of the:: operator), and the class to which the object belongs when a Non-static method call is made. The so-called "forward invocation" (forwarding call) refers to static calls made in the following ways: self::,parent::,static:: and Forward_static_call (). The Get_called_class () function can be used to get the class name of the method being invoked, static:: The scope is pointed out.
For an understanding of this feature, refer to the example in the following manual
Self vs Static
Use a demo to directly illustrate the difference between self and static.
Self Example:
The code is as follows:
Class Vehicle {
protected static $name = ' This is a Vehicle ';
public static function What_vehicle () {
Echo Get_called_class (). n ";
echo self:: $name;
}
}
Class Sedan extends Vehicle {
protected static $name = ' This is a sedan ';
}
Sedan::what_vehicle ();
Program output:
The code is as follows:
Sedan
This is a Vehicle
static Example:
The code is as follows:
Class Vehicle {
protected static $name = ' This is a Vehicle ';
public static function What_vehicle () {
Echo Get_called_class (). n ";
echo Static:: $name;
}
}
Class Sedan extends Vehicle {
protected static $name = ' This is a sedan ';
}
Sedan::what_vehicle ();
Program output:
The code is as follows:
Sedan
This is a sedan
Summarize
Look at an article, has not updated one months blog, busy is part of the main or their own slack, and later have to insist. This article is also a little bit insensitive.