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:
Copy Code code 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:
Copy Code code 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:
Copy Code code as follows:
<?php
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:
Copy Code code as follows:
static Example:
Copy Code code as follows:
<?php
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:
Copy Code code as follows:
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 adhere to. This article is also a little bit insensitive.