PHP中new static()与new self()的区别!

self:始终指向self代码所在的类的本身,无论这个类被继承了多少次,self都指向初使用self的类

static:指向使用static的类,只有通过继承后,才能体现出static的意义,否则static和self一样

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_self()); // A
echo get_class(A::get_static()); // A

stackoverflow:New self vs. new static

此处评论已关闭