This article introduces the content of "PHP class and object" Late static binding, has a certain reference value, now share to everyone, the need for friends can refer to
Late static binding
PHP 5.3.0
For: Classes in the inheritance scope that refer to static calls
Working principle:
The class name stored in the previous "non-forwarding call" (Non-forwarding calls).
When a static method call is made, the class name is the one explicitly specified (usually in the left part of the: operator);
When a non-static method call is made, the class to which the object belongs.
Forwarding calls (forwarding call): Static calls through Self::,parent::,static::, Forward_static_call () (Calling a static method).
You can use the Get_called_class () function to get the class name of the method being called, Static:: It indicates its scope.
This feature is named "Late static binding" from the perspective of the language itself.
Late binding means that static:: It 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.
<?phpclass A {public static function who () { echo __class__;//Returns the name of the class when it is defined } public static function t EST () { self::who (); }} Class B extends A { //public static function who () { // echo __class__; }}b::test (); A?>
Use of late static bindings
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.
Example #2 static:: Simple usage <?phpclass A {public static function who () { echo __class__; } public static function test () { static::who ();//note using static, late static binding starts here }}class B extends A {public stat IC function who () { echo __class__; }} B::test (); B?>
In a non-static environment, the class that is called is the class to which the object instance belongs. Because $this, which attempts to invoke private methods within the same scope, static:: You might give different results.
Another difference is static:: Only for static properties.
Late static binding
PHP 5.3.0
For: Classes in the inheritance scope that refer to static calls
Working principle:
The class name stored in the previous "non-forwarding call" (Non-forwarding calls).
When a static method call is made, the class name is the one explicitly specified (usually in the left part of the: operator);
When a non-static method call is made, the class to which the object belongs.
Forwarding calls (forwarding call): Static calls through Self::,parent::,static::, Forward_static_call () (Calling a static method).
You can use the Get_called_class () function to get the class name of the method being called, Static:: It indicates its scope.
This feature is named "Late static binding" from the perspective of the language itself.
Late binding means that static:: It 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.
<?phpclass A {public static function who () { echo __class__;//Returns the name of the class when it is defined } public static function t EST () { self::who (); }} Class B extends A { //public static function who () { // echo __class__; }}b::test (); A?>
Use of late static bindings
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.
Example #2 static:: Simple usage <?phpclass A {public static function who () { echo __class__; } public static function test () { static::who ();//note using static, late static binding starts here }}class B extends A {public stat IC function who () { echo __class__; }} B::test (); B?>
In a non-static environment, the class that is called is the class to which the object instance belongs. Because $this, which attempts to invoke private methods within the same scope, static:: You might give different results. The
other difference is static:: Only for static properties.