The content of this article is PHP late binding, now share to everyone, the need for friends can also refer to this article
Late PHP static bindings
Since PHP 5.3.0, PHP has added a feature called post-static binding that is used to
A class that is called statically.
To be exact, late static binding works by storing the previous "non-forwarding call" (non-forwarding
Call) 's class name. When a static method call is made, the class name is the one explicitly specified (usually in::
The left part of the operator), or the class to which the object belongs when a non-static method call is made. The so-called "forwarding call" (forwarding
Call) refers 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:
The range is indicated.
This feature is named "Late static binding" from the perspective of the language itself. The word "late binding" means, Static::
is no longer parsed to define the class in which the current method resides, but is calculated at the actual run time. It can also be called a "static binding" because it can be used (but not limited to) the invocation of a static method.
Self
Use self:: or a static reference to the current class, depending on the class in which the current method is defined:
Example #1 Self:: Usage
<?phpclass A {public static function who () { echo __class__; } public static function test () { self::who (); }} Class B extends A {public static function who () { echo __class__; }} B::test (); Output a?>
Use of static bindings later in the static
Late static binding This is to bypass the restriction by introducing a new keyword that represents the class that was originally called by the runtime. Simply put, this keyword allows you to invoke test () in the above example to refer to the class B instead of a. The final decision is not to introduce a new keyword, but instead to use the static keyword that has been reserved.
<?phpclass A {public static function who () { echo __class__; } public static function test () { static::who ();//Late static binding starts here }}class B extends A {public static function Who () { echo __class__; }} B::test (); Output b?>
Note:
In a non-static environment, the class that is called is the class to which the object instance belongs. Because $this, the private method is attempted within the same scope, and static::
may give different results. Another difference is static:: Only for static properties.
Use static in a non-static environment:
<?phpclass A { private function foo () { echo "success!\n"; } Public Function test () { $this->foo (); Static::foo (); }} Class B extends A { /* foo () would be copied to B, hence its scope would still be A and * The call is successful */} Class C extends A { private function foo () {/ * original method is replaced; the scope of the new one is C */
}} $b = new B (); $b->test (); $c = new C (); $c->test (); Fails?>
The above routines will output:
success! success! success!
Fatal Error:call to Private Method C::foo () from the context ' A ' in/tmp/test.php on line 9
Note:
The parsing of a late static binding continues until a fully resolved static call is made. On the other hand, if a static call uses parent:: or self:: The call message is forwarded.
Forwarding and non-forwarding calls
<?phpclass A {public static function foo () { static::who (); } public static function who () { echo __class__. \ n "; }} Class B extends A {public static function test () { a::foo (); Parent::foo (); Self::foo (); } public static function who () { echo __class__. \ n "; }} Class C extends B {public static function who () { echo __class__. \ n "; }} C::test ();? >
The above routines will output:
A c C