This article mainly introduces the definition of static keywords in PHP, the late binding and the difference with the Self keyword, interested in the friend's reference, I hope to help you.
Overview
is learning design patterns, there is an article about the singleton mode of the article, reread this article, found that the Static keyword mastery is not very strong, re-review.
Static keyword
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 by operator.
3. In the scope of a static method, the $this keyword cannot appear, which means that normal member variables cannot be accessed in a static method.
4. Static members and methods can be accessed directly through the class name without having to instantiate the object.
Late binding (late Static Bindings)
Since PHP 5.3.0, PHP has added a feature called late static binding that references statically invoked classes within the scope of the inheritance.
To be exact, late static binding works by storing the class name on the previous "non-forwarding call" (Non-forwarding calls). When a static method call is made, the class name is the one that is explicitly specified (usually in the left part of the: operator), and the class to which the object belongs when a non-static method call is made. The so-called "forwarding calls" (forwarding call) refer to static calls made in the following ways: self::,parent::,static:: and Forward_static_call (). You can use the Get_called_class () function to get the class name of the method being called, Static:: It indicates its scope.
For an understanding of this feature, you can refer to the example in the following manual
Self vs Static
Use a demo to directly explain the difference between self and static.
Self Example:
The code is as follows:
<?phpclass 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:
Sedanthis is a Vehicle
static Example:
The code is as follows:
<?phpclass 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:
Sedanthis is a Sedan
Summary : The above is the entire content of this article, I hope to be able to help you learn.
Related recommendations:
Ways to parse HTML files using the Snoopy class
Three common uses of PHP analog post requests
The definition and implementation of PHP for event mechanism