Login
网站首页 > 文章中心 > 其它

php延迟静态绑定的深入讲解

作者:小编 更新时间:2023-09-17 15:08:34 浏览量:317人看过

PHP Static延迟静态绑定用法分析

本文实例讲述了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 是什么?

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.

如何理解php中的后期静态绑定

使用的保留关键字:

定义:

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延迟静态绑定的深入讲解相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章