【发布时间】:2021-04-08 17:04:21
【问题描述】:
class ParentClass
{
private function foo()
{
echo 'foo() in ParentClass';
}
public function test()
{
echo $this::class; // ChildClass
$this->foo();
}
}
class ChildClass extends ParentClass
{
private function foo()
{
echo 'foo() in ChildClass';
}
}
$o = new ChildClass();
$o->test(); // 'foo() in ParentClass';
我希望结果是 'foo() in ChildClass'。
我的问题是为什么 $this->foo(); 在 test() 中调用 foo() 在 ParentClass 而不是在 ChildClass 中?为什么它没有被覆盖?
【问题讨论】:
标签: php oop inheritance