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

  1. public static function getInstance()
  2. {
  3. if (is_null(static::$instance)) {
  4. static::$instance = new static;
  5. }
  6. return static::$instance;
  7. }

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

由来

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

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

class B extends A{ public static function who(){ echo CLASS.’3
‘; echo get_called_class(),’5
‘; } }

class C extends B{

} $c = new C(); C::call(); //结果是B3,C5

  1. ```php
  2. class A{
  3. public static function call(){
  4. static::who();
  5. }
  6. public static function who(){
  7. echo __CLASS__,'1<br>';
  8. echo get_called_class(),'2<br>';
  9. }
  10. }
  11. class B extends A{
  12. public static function who(){
  13. echo __CLASS__.'3<br>';
  14. echo get_called_class(),'5<br>';
  15. }
  16. }
  17. class C extends B{
  18. public static function who(){
  19. echo __CLASS__.'6<br>';
  20. echo get_called_class(),'7<br>';
  21. }
  22. }
  23. $c = new C();
  24. 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()