官网说道:

As of PHP 5.3.0, PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance.

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 that static:: 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.

Limitations of self::

Static references to the current class like self:: or __CLASS__ are resolved using the class in which the function belongs, as in where it was defined:

Example #1 self:: usage、

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。

Late Static Bindings' usage

Late static bindings tries to solve that limitation by introducing a keyword that references the class that was initially called at runtime. Basically, a keyword that would allow you to reference B from test() in the previous example. It was decided not to introduce a new keyword but rather use static that was already reserved.

Example #2 static:: simple usage

<?php
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // Here comes Late Static Bindings
}
} class B extends A {
public static function who() {
echo __CLASS__;
}
} B::test();
?>

输出B;

Note:

In non-static contexts, the called class will be the class of the object instance. Since $this-> will try to call private methods from the same scope, using static:: may give different results. Another difference is thatstatic:: can only refer to static properties.

Example #3 static:: usage in a non-static context

class A {
private function foo() {
echo "success!\n";
}
public function test() {
$this->foo();
static::foo();
}
} class B extends A {
/* foo() will be copied to B, hence its scope will still be A and
* the call be successful */
} class C extends A {
private function foo() {
/* original method is replaced; the scope of the new one is C */
}
} $b = new B();
$b->test();
$c = new C();
$c->test(); //fails

success! success! success!
( ! ) Fatal error: Call to private method C::foo() from context 'A' in F:\xampp\htdocs\php\phpSyntax\lateStaticBinding.php on line 26

更多:

http://cn2.php.net/lsb

php Late Static Bindings延迟静态绑定的更多相关文章

  1. PHP "延迟静态绑定" 功能,static

    从这个名字的定义提取出两个关键点,第一点静态,也就是说这个功能只适用于静态属性或静态方法.第二点延迟绑定,这个根据下面代码就可以很好的理解 看一下这个例子: class A{ static $name ...

  2. PHP延迟静态绑定 static关键字

    示例代码1 abstract class Parent { } class Man extends Parent { public static function create(){ return n ...

  3. PHP延迟静态绑定:static关键字

    PHP5.3中引入了延迟静态绑定的概念.该特性最明显的标志就是新关键字static.static类似于self,但它指的是被调用的类而不是包含类.在本例中,它的意思是调用Document::creat ...

  4. php static延迟静态绑定

    如果你是一个懒惰的程序员,你看到以下代码可能会恼火 abstract class U{ } class u1 extends U{ public static function create(){ r ...

  5. php5.3 延迟静态绑定 static关键字

    //传统模式 --这段代码能很好工作,但大量的重复代码很烦人,不想为每个DomainObject子类都创建这段相同代码吧? /* abstract class DomainObject{} class ...

  6. get_called_class--后期静态绑定("Late Static Binding")类的名称

    get_called_class--后期静态绑定("Late Static Binding")类的名称 string get_called_class ( void ) 获取静态方 ...

  7. PHP延迟静态绑定

    类可以自下往上调用父类方法,如果需要在父类中根据不同的子类,来调用子类的方法,那么就需要延迟静态绑定.延迟静态绑定用的是保留关键词static. 所谓延迟静态绑定,顾名思义,静态调用时::符号左侧的部 ...

  8. 父类方法返回子类实例:PHP延迟静态绑定

    案例分析 先前的PHP项目中,看到类似于以下的一段代码: <?php class DBHandler { public function get() { } } class MySQLHandl ...

  9. PHP延迟静态绑定(本文属于转发)

    这段时间看项目后台的PHP代码,看到了类似于以下的一段代码,我把它抽出来: <?php class DBHandler { function get() {} } class MySQLHand ...

随机推荐

  1. 按按钮调用PHP function函数

    首先,请大家看一段HTML代码: <html> <head> </head> <body> <input type=button on_click ...

  2. Poj 2996 Help Me with the Game

    1.Link: http://poj.org/problem?id=2996 2.Content: Help Me with the Game Time Limit: 1000MS   Memory ...

  3. WinSCP列出’/’目录项出错

    无法获得目录列表 如图所示,使用百度云虚拟机时,FTP连接服务器,出现错提示,官方给出的说法是使用其他的FTP进行连接,但是之前成功连接过,查找资料后说是打开过png,mp3等媒体文件,切换路径时出错 ...

  4. js数字格式化-四舍五入精简版

    搜索网上的,数字格式化过余复杂,自己想了个简单方法,欢迎吐槽. 简化说明: '123333' => 12.3万 parseInt('123333') 字符串转整型 parseInt('12333 ...

  5. ubuntu更新源,简单两步搞定

    1.启动器中打开Ubuntu软件中心 2.鼠标顶部面板点击编辑选择软件源(163.sohu) 搞定!

  6. CorelDRAW 二维码插件

    随着智能手机的流行,二维码在各个领域大量应用,这个插件在补CorelDRAW这方面的不足: 这个插件是 cpg 格式,安装请看这篇博客:http://www.cnblogs.com/o594cql/p ...

  7. DHT网络第一部分研究结果 加入长期在线的node

    源码:http://jijiea.com/upfile/DHT_Part1_How_To_Join_In_DHT.zip

  8. vs2010配备boost编程环境

    vs2010配备boost编程环境 vs2010配置boost编程环境 第一步:下载boost,我下载的方法是从http://www.boost.org/上找最新的下载.名字叫boost_1_53_0 ...

  9. H264学习第一篇(编码结构分析)

    学习H264之前,最好阅读一下维基百科中有关H264的相关介绍,里面包含了其的发展历程.主要特点.参考文献.参考网站等. 研究H264的主要文件包括两份参考手册(一份是语法结构参考手册,一份是JM开发 ...

  10. PHP - PDO 之 mysql 参数绑定

    <?php /* pdo 学习 */ $dsn = 'mysql:host=localhost;dbname=cswl';//构建连接dsn $db = new pdo($dsn,'root', ...