0
class ParentClass
{
    private function first(): void
    {
        p(__METHOD__);
    }

    public function test(): void
    {
        static::first();

        $this->first();

        $B = new B();
        $B->first();
    }
}

class B extends ParentClass
{
    public function first(): void
    {
        p(__METHOD__);
    }
}

$B = new B();
$B->test();
B::first

ParentClass::first

ParentClass::first

Why is it that having a reference to B in $this and having the public access modifier for the first() method, the method with the private modifier in ParentClass is called? But if we allow inheritance by removing private, then the method from B will be called.

Doesn't this look illogical?

I expected that the B::first() method would be called at least for a direct call. It may not be able to find first() in class B because it goes down the reverse inheritance chain, and this method in B does not override the first function from ParentClass.

Those. in order for it to be called from the scope of ParentClass, it must necessarily override the method and not just match the name.

0 Answers0