The Static keyword in PHP and the difference from the Self keyword
This article mainly introduces the static keyword in PHP and the difference from the Self keyword, this article explains the definition of the static keyword, late binding (late static Bindings), and the differences with the Self keyword, and so on, the need for friends can refer to the following
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 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 an instantiated class object (though a static method can).
The general meaning is that when you declare a class's properties and methods as static, you can directly access static properties and methods 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 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)
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 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:
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 the previous article, has not updated the blog for one months, busy is part of the main or their own slack, and then have to insist. This article is also a little bit not feeling.
http://www.bkjia.com/PHPjc/1025331.html www.bkjia.com true http://www.bkjia.com/PHPjc/1025331.html techarticle The Static keyword in PHP and the difference from the self keyword This article mainly introduces the static keyword in PHP and the difference from the Self keyword, this article explains the definition of the static keyword, late binding ...