官网说道:

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. java学习笔记_GUI(5)

    demo如何为不同的button创建对应的响应函数 import javax.swing.*; import java.awt.event.*; import java.awt.*; class My ...

  2. Knockout.js 初探

    Knockout.js是什么? Knockout是一款很优秀的JavaScript库,它可以帮助你仅使用一个清晰整洁的底层数据模型(data model)即可创建一个富文本且具有良好的显示和编辑功能的 ...

  3. [DevExpress]RepositoryItemComboBox 数据绑定

    关键代码: public static void Bind<T>(this RepositoryItemComboBox combox, ICollection source) { /*说 ...

  4. Java Thread and runnable

    java中可有两种方式实现多线程, 一种是继承Thread类,(Thread本身实现了Runnable接口,就是说需要写void run 方法,来执行相关操作) 一种是实现Runnable接口 sta ...

  5. Review PHP设计模式之——观测模式

    观测模式: <?php class car implements SplSubject{ private $carName; //车的类型 private $carState=0; //车的状态 ...

  6. 几种placeholder替换项目参数的方法比较

    引言:(引自:http://openwebx.org/docs/autoconfig.html) 在一个应用中,我们总是会遇到一些参数,例如: 数据库服务器IP地址.端口.用户名: 用来保存上传资料的 ...

  7. 淘宝的ip地址库

    1. 请求接口(GET): http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串] 2. 响应信息: (json格式的)国家 .省(自治区或直辖市 ...

  8. xaml中绑定单例属性

    在项目中经常会遇到,同一个字典表绑定到多个ItemsControl上的情况,可以在单例中创建一个List,xaml上绑定即可.看代码: 1,XAML <Grid> <StackPan ...

  9. 【转】perl特殊符号及默认的内部变量

    perl特殊符号及默认的内部变量,有需要的朋友不妨参考下 Perl的特殊符号 @       数组                          $x{}   x名字前面是美元符号($),后面是花 ...

  10. 九度OJ - 题目1481:Is It A Tree?

    题目描述: A tree is a well-known data structure that is either empty (null, void, nothing) or is a set o ...