php后期静态绑定

2017-07-10  本文已影响38人  winlans

在观看laravel源码的时候,突然看到了

public static function getInstance()
    {
        if (is_null(static::$instance)) {
            static::$instance = new static;
        }

        return static::$instance;
    }

虽然之前翻看手册的时候看到过,但很少使用,于是仔细看了一下,原来是后期静态绑定。但是后期静态绑定又是什么鬼,还是仔细分析下吧。

由来

PHP在继承体系中,调用分为转发调用和非转发调用。

  1. 使用 self:: 或者 __CLASS__ 对当前类的静态引用,取决于定义当前方法所在的类。
  2. 使用 parent:: 表示对当前类的父类的静态引用。
  3. static:: “后期绑定”的意思是说,static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为“静态绑定”,因为它可以用于(但不限于)静态方法的调用。
    这是什么意思呢?还是通过代码来说比较好,哈哈
class A{
    public static function call(){
       static::who();
    }
    public static function who(){
        echo __CLASS__,'1<br>';
        echo get_called_class(),'2<br>';
    }
}

class B extends A{
    public static function who(){
        echo __CLASS__.'3<br>';
        echo get_called_class(),'5<br>';
    }
}

class C extends B{

}
$c = new C();
C::call();  //结果是B3,C5
class A{
    public static function call(){
       static::who();
    }
    public static function who(){
        echo __CLASS__,'1<br>';
        echo get_called_class(),'2<br>';
    }
}

class B extends A{
    public static function who(){
        echo __CLASS__.'3<br>';
        echo get_called_class(),'5<br>';
    }
}

class C extends B{
    public static function who(){
        echo __CLASS__.'6<br>';
        echo get_called_class(),'7<br>';
    }
}
$c = new C();
C::call(); //结果是C6,C7。
class A{
   static $instance;
}
class B extends A{
    public static function put(){
       return static::$instance = new static ;
    }
}
class C extends B{

}
var_dump(C::put()); // object(C)#1 (0) { }
var_dump(B::put()); // object(B)#1 (0) { }

不知道大家有没有看懂,我认为,static::类似于thisthis是对当前对象的引用,而static::是对当前调用类的引用。

  1. 直接使用类名:: fun()
上一篇 下一篇

猜你喜欢

热点阅读