Search code examples
phpinheritancephpstorm

Is it a bad sign not to call the parent's method from the overridden method?


class A
{
    public function getName()
    {
        return 'First class';
    }
}

class B extends A
{
    public function getName()
    {
        return 'Second class';
    }
}

Phpstorm then warns me and highlights B::getName() that I missed to call its parent: parent::getName(); I intentionally don't call it. But this leads me the conclusion that its a sign of something bad.


Solution

  • The inspection is located in a Probable bugs inspections group - it means that's not an error and not something bad that IDE is sure about. The inspection itself is disabled by default, you might've enabled it on your own. The idea of this inspection is described here:

    Generally, when you override a method (especially a constructor), you want to call the parent method in it. An info inspection for lack of parent:: call could be handy