0
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';

I expect the result is 'foo() in ChildClass'.

My question is why $this->foo(); inside test() call foo() in ParentClass rather in ChildClass? Why it is not get overriden?

1 Answers1

1

Replace your private visibility modificators with protected. Private methods are not reusable and therefore hidden from classes that extend them.

user1597430
  • 1,138
  • 1
  • 7
  • 14