我贴一段代码 这个问题老外解释的比较好 = =
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
new self 那么获取的是当前代码段的这个类。 这个在class A 中写的方法 就算你在class B 中继承了,调用时返回的还是 class A 中的代码
new static PHP 5.3新增的方法,有点 $this 的味道,调用对应的 class 就返回 对应 class 的方法。