PHP中new static()与new self()的区别
self:始终指向self代码所在类的本身,无论这个类被继承了多少次,self都指向最初使用self的类;
static:指向使用static的类,只有通过继承后,才能体现出static存在的意义,否则static和self一样。
class A {
public function getStatic(){
return new static();
}
public function getSelf(){
return new self();
}
}
class B extends A{
}
var_dump((new B())->getSelf());//A
var_dump((new B())->getStatic());//B
疑问:
1、不知道new static()和new self()有什么用;
2、如果要使用new self(),为何不使用new A();