PHP中new self()和new static()的区别
作者:admin 发布时间:July 19, 2018 分类:PHP
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类