Home >Backend Development >PHP Tutorial >PHP延迟静态捆绑Late Static Bindings

PHP延迟静态捆绑Late Static Bindings

WBOY
WBOYOriginal
2016-05-19 15:12:071988browse
看了一下php5.3的Late Static Bindings,简单了翻译一下
Late Static Bindings是在PHP5.3中加入的新特性,拼音来说,就是把本来在定义阶段固定下来的表达式
或变量,改在执行阶段才决定,比如当一个子类继承了父类的静态表达式的时候,它的值并不能被改变,有
时不希望看到这种情况
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
?>
但是现在我想让其输出B,那么使用Late Static Bindings可实现这一特性了
class A {
   public static function who() {
     echo __CLASS__;
   }
   public static function test() {
     static::who(); // Late Static Bindings  
   }
}
class B extends A {   
   public static function who() {
     echo __CLASS__;
   }

B::test();//输出B
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn