One: late PHP static binding
Starting with php5.3, PHP adds a feature called late binding that references statically invoked classes within the scope of the inheritance
This feature is named "Late static binding" from the perspective of the language, and "late binding" means: Static:: It is no longer parsed to define the class in which the current method is located, but is calculated at the actual run time, and can also be a "static binding", because he can be used (but not limited to static method invocation).
Self:: the limit
Use self:: or _class_ a static reference to the current 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 (); A
Print Result: A
Use of late static bindings
Late static binding attempts to bypass the restriction by introducing a keyword that represents the class that was originally called by the runtime. Simply put, this keyword allows you to refer to the Class B instead of a When you call Test () in the above. The final decision does not refer to the new keyword, but instead uses the reserved static keyword
Example:
<?phpclass A {public static function who () {echo __class__; The public static function test () {static::who ();///Late static binding starts here}}} class B extends A {public static funct Ion who () {echo __class__; }} b::test ();? >
The printing result is: B
Late static binding is handled in a way that solves static calls that have not been resolved in the past, and on the other hand, if a static call uses the parent:: or self:: forwards the call information
<?phpclass A { public static function foo () { static::who (); } public static function who () { echo __class__. \ n "; }} class B extends A { public static function test () { & Nbsp; 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 ();? >
Printing results:
A c C
Example:
Printing results:
A
B
Special statement:
Get_called_class (); Gets the name of the class called in a static method
<?php
Class 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 ();
?>
Share nine: PHP easy to confuse syntax