self refers to the same class whose method the new operation takes place in.
static in PHP 5.3’s late static bindings refers to whatever class in the hierarchy which you call the method on.
self:: 或者 (_ _ CLASS_ _) 对当前类的静态引用,取决于定义当前方法所在的类:
static:: 不再被解析为定义当前方法所在的类,而是在实际运行时计算的。也可以称之为“静态绑定”,因为它可以用于(但不限于)静态方法的调用。
举例来说
<?php
class A {
public static function getSelf() {
return new self();
}
public static function getStatic() {
return new Static();
}
}
class B extends A {}
echo get_class(B::getSelf()); // A
echo get_class(B::getStatic()); // B
echo get_class(A::getSelf()); // A
echo get_class(A::getStatic()); // A
self 返回的是 new self 中关键字 new 所在的类中
static 会返回执行 new static() 的类
在没有继承的情况下,new self 和 new static 返回结果一样。