PHP中new static() 和 new self() 的区别

self 指的是self所在的类

new static 实例化的是当前使用的类,有点像$this ,从堆内存中提取出来。

还是通过实例说明一下:

class A {
    public static function get_self() {
        return new self();
    }

 

    public static function get_static() {
        return new static();
    }
}

class B extends A {}

echo get_class(B::get_self());  // A
echo get_class(B::get_static()); // B
echo get_class(A::get_static()); // A

posted @ 2017-07-04 22:46  Burning_Leaf  阅读(367)  评论(0编辑  收藏  举报