The method of realizing static binding in later stage by php
This article mainly introduces the method of static binding in the later stage of php, which has a certain reference value and can be used for reference by friends who need it. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.
Late static binding works by storing the class name of the previous "non-forwarded call" (non-forwarding call). When making a static method call, the class name is the one explicitly specified (usually in the left part of the:: operator); when a non-static method call is made, it is the class to which the object belongs.
The so-called "forward call" (forwarding call) refers to static calls made in several 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, and static:: indicates its scope.
Limitations of self::
A static reference to the current class using self:: or _ _ CLASS__, depending on the class in which the current method is defined:
Example:
Class 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 ()
Results:
Astatic (later static binding)
Late static binding was intended to bypass the limitation by introducing a new keyword to represent the class originally called by the runtime. Simply put, this keyword allows you to refer to class B instead of A when you call test () in the above example. In the end, it was decided not to introduce new keywords, but to use the reserved static keyword.
Example:
Results:
The difference between Bstatic and $this
In a non-static environment, the class invoked is the class to which the instance of the object belongs. Because $this- > tries to call private methods in the same scope, static:: may give different results. Another difference is that static properties can only be called with static::.
Example: calling a private method