php Late Static Bindings延迟静态绑定

官网说道:

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延迟静态绑定

时间: 2024-10-12 21:56:07

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

【转】PHP中的后期静态绑定(Late Static Bindings )

php5.3版本新增的静态绑定的静态绑定语法,也成为PHP的后期静态绑定,如下 class A{ public static function func1(){ echo __CLASS__.PHP_EOL; } public static function test(){ self::func1(); } } class B extend A{ public static function func1(){ echo __CLASS__.PHP_EOL; //__CLASS__是当前类的名称

PHP延迟静态绑定

php5.3已经开始支持延迟静态绑定. 延迟静态绑定指的是在父类中获取子类的最终状态.在父类中,如果出现self关键字,被子类继承后,这个self值的还是父类而不是子类. 如果在父类中出现了self关键字,并且子类继承了含有self的这段代码,那么需要考虑静态延迟绑定.在父类中使用static代替self 如下例: <?php class A{ public static $a=1; public function test(){ echo self::$a;//自身的属性a } } class

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

PHP5.3中引入了延迟静态绑定的概念.该特性最明显的标志就是新关键字static.static类似于self,但它指的是被调用的类而不是包含类.在本例中,它的意思是调用Document::create()将生成一个新的Document对象,而不是试图实例化一个DomainObject对象. 因此,现在在静态上下文中使用继承关系. abstract class DomainObject{ public static function create(){ return new static();

PHP Static延迟静态绑定

初识PHP Static延迟静态绑定 PHP5.3以后引入了延迟静态绑定static,它是为了解决什么问题呢?php的继承模型中有一个存在已久的问题,那就是在父类中引用扩展类的最终状态比较困难.来看一个例子. PHP 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class A { public static function echoClass(){ echo __CLASS__; } public static function tes

PHP延迟静态绑定 static关键字

示例代码1 abstract class Parent { } class Man extends Parent { public static function create(){ return new Man(); } } class Woman extends Parent { public static function create(){ return new Woman(); } } 示例代码有一个parent父类,还包含Man和Woman两个子类,并在该两个类中都包含创建本身的的静

get_called_class--后期静态绑定(&quot;Late Static Binding&quot;)类的名称

get_called_class--后期静态绑定("Late Static Binding")类的名称 string get_called_class ( void ) 获取静态方法调用的类名. 返回类的名称,如果不是在类中调用则返回 FALSE. <?php class Person{    public $username;    public $age;    public $height;    public $weight;    static public $numb

PHP &quot;延迟静态绑定&quot; 功能,static

从这个名字的定义提取出两个关键点,第一点静态,也就是说这个功能只适用于静态属性或静态方法.第二点延迟绑定,这个根据下面代码就可以很好的理解 看一下这个例子: class A{ static $name = "Tom"; public function printName(){ echo self::$name."\n"; self::fun(); } static function fun(){ echo "A Class\n"; } } cla

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

1 //传统模式 --这段代码能很好工作,但大量的重复代码很烦人,不想为每个DomainObject子类都创建这段相同代码吧? 2 /* 3 4 abstract class DomainObject{} 5 6 class User extends DomainObject 7 { 8 public function __construct() 9 { 10 echo __METHOD__; 11 } 12 13 public static function create() 14 { 15

“延迟静态绑定”的使用

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 <?php class ParentBase { static $property = 'Parent Value'; public static function render() { return self::$property; } } class Descendant extends P