本文实例讲述了PHP
Static延迟静态绑定用法.分享给大家供大家参考,具体如下:
class
A
{
public
static
function
echoClass(){
echo
__CLASS__;
}
test(){
self::echoClass();
B
extends
echoClass()
B::test();
//输出A
下面的例子解决了上面提出的问题:
test()
static::echoClass();
//输出B
更多关于PHP相关内容感兴趣的读者可查看本站专题:<
希望本文所述对大家PHP程序设计有所帮助.
PHP延迟静态绑定 Late Static Binding
推荐阅读以下内容:
More precisely, late static bindings work by storing the class named in the last "non-forwarding call". In case of static method calls, this is the class explicitly named (usually the one on the left of the :: operator); in case of non static method calls, it is the class of the object. A "forwarding call" is a static one that is introduced by self::, parent::, static::, or, if going up in the class hierarchy, forward_static_call(). The function get_called_class() can be used to retrieve a string with the name of the called class and static:: introduces its scope.
This feature was named "late static bindings" with an internal perspective in mind. "Late binding" comes from the fact thatstatic:: will not be resolved using the class where the method is defined but it will rather be computed using runtime information. It was also called a "static binding" as it can be used for (but is not limited to) static method calls.
使用的保留关键字:
定义:
static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的.也可以称之为"静态绑定",因为它可以用于(但不限于)静态方法的调用.
self与static的区别:
self调用的就是本身代码片段这个类,而static调用的是从堆内存中提取出来,访问的是当前实例化的那个类(即static作用于当前调用的类)
示例一(在静态环境下)
phpclass A { public static function who() { echo __CLASS__;
} public static function test() { static::who(); // 后期静态绑定从这里开始
}class B extends A { public static function who() { echo __CLASS__;
B::test();?输出结果是"B"
以上就是土嘎嘎小编为大家整理的php延迟静态绑定的深入讲解相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!