This article introduces the content of [php classes and objects] late static binding, which has a certain reference value. Now I share it with you. Friends in need can refer to it
Later static binding
PHP 5.3.0
Used for: Reference to statically called classes in the inheritance scope
Working principle:
stored The class name of the last "non-forwarding call".
When making a static method call, the class name is the one explicitly specified (usually on the left side of the :: operator);
When a non-static method call is made, it is the class to which the object belongs.
Forwarding call: static call 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 called method, and static:: points out its scope.
This feature is named "late static binding" from a language internal perspective.
"Late binding" means that static:: is no longer parsed into the class in which the current method is defined, but is calculated at actual runtime.
It can also be called "static binding" because it can be used for (but is not limited to) calls to static methods.
Usage of late static binding
Late static binding originally wanted to circumvent this by introducing a new keyword to represent the class initially called at runtime. over restrictions.
Simply put, this keyword allows you to refer to class B instead of A when calling test() in the above example. It was finally decided not to introduce new keywords, but to use the already reserved static keyword.
Example #2 static:: 简单用法
In a non-static environment, the class called is the class to which the object instance belongs. Since $this-> will try to call the private method in the same scope, static:: may give different results.
Another difference is that static:: can only be used for static properties.
Late static binding
PHP 5.3.0
Used for: referencing statically called classes in the inheritance scope
Working principle:
Stores the class name of the previous "non-forwarding call".
When making a static method call, the class name is the one explicitly specified (usually on the left side of the :: operator);
When a non-static method call is made, it is the class to which the object belongs.
Forwarding call: static call 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 called method, and static:: points out its scope.
This feature is named "late static binding" from a language internal perspective.
"Late binding" means that static:: is no longer parsed into the class in which the current method is defined, but is calculated at actual runtime.
It can also be called "static binding" because it can be used for (but is not limited to) calls to static methods.
Usage of late static binding
Late static binding originally wanted to circumvent this by introducing a new keyword to represent the class initially called at runtime. over restrictions.
Simply put, this keyword allows you to refer to class B instead of A when calling test() in the above example. It was finally decided not to introduce new keywords, but to use the already reserved static keyword.
Example #2 static:: 简单用法
In a non-static environment, the class called is the class to which the object instance belongs. Since $this-> will try to call the private method in the same scope, static:: may give different results.
Another difference is that static:: can only be used for static properties.
Related recommendations:
[php classes and objects]Type constraints
[php classes and objects]Object copy
【php classes and objects】magic method
The above is the detailed content of [php classes and objects] late static binding. For more information, please follow other related articles on the PHP Chinese website!