PHP new self 和 new static 有什么区别:
测试代码
<?php
class Father
{
public function getSelf()
{
return new self();
}
public function getStatic()
{
return new static();
}
}
class Son1 extends Father
{
public function getName()
{
echo "My name is Son1";
}
}
$son1 = new Son1;
print_r($son1->getSelf());
print_r($son1->getStatic());
结果如图
结论:
new self 返回该方法所在类的类实例;
new static 返回抵用该方法所在的类的类实例;