0

This works:

abstract class WonderfulBaseClass
{
    public function doStuff()
    {
        $this->doPart1();
        $this->doPart2();
    }

    abstract protected function doPart1();
    abstract protected function doPart2();
}

but this doesn't (IDE says "Cannot call abstract method doPart[...]"):

abstract class WonderfulBaseClass
{
    public static function doStuff()
    {
        static::doPart1();
        static::doPart2();
    }

    abstract protected static function doPart1();
    abstract protected static function doPart2();
}

My thought was that extenders of the abstract base-class would still be forced to implement doPart1 and doPart2 so there shouldn't be a problem but I guess I have a fundamental misunderstanding going on.

Can someone explain why the first case is fine while the second seems to be problematic?

EDIT: changed self to static in the second part -> yields the same error message

Wolfone
  • 1,276
  • 3
  • 11
  • 31
  • 2
    `self` always refers to `WonderfulBaseClass`. You want `static::doPart1()`. – deceze Mar 24 '23 at 10:21
  • `static::doPart1()` produces the same error-message. I edited the question accordingly. – Wolfone Mar 24 '23 at 10:32
  • 1
    It's just your IDE complaining? Does it produce any complaints during actual execution? What is your IDE/linter that produces this message? Might simply be a limitation with it. – deceze Mar 24 '23 at 10:33
  • Ha! Very good hint! The following seemed to be the problem with the error-hint. I introduced the static-template method in the base class but the extending class didn't align with the updated interface yet and it seems that PhpStorm recognizing that, showed this error in the base-class. The error-message is just a bit unlucky in my opinion. Now life makes sense again :D And thanks again for the clear-up about the static/self difference! – Wolfone Mar 24 '23 at 10:40

0 Answers0