range

If there were you, the world would be just right

1、区别只有在继承中才能体现出来,如果没有任何继承,那么这两者是没有区别的。
2、new self()返回的实例是万年不变的,无论谁去调用,都返回同一个类的实例,而new static()则是由调用者决定的。
例子:

<?
class Father {

    public function NewSelf() {
        return new self();
    }

    public function Newstatic() {
        return new static();
    }
}
class Sun extends Father {

}
$sun = new Sun();

print get_class($sun->NewSelf());     //=> 返回的是Father类
print get_class($sun->Newstatic());  //=> 返回的是sun类  

添加新评论 »

在这里输入你的评论...