How can I call a non-static function from an abstract class in PHP 8?

I want to update our current project that runs on PHP 7.2 but I got an issue that I cant resolve easily.

public function getCategories($type){...}

//In another file that dosent implements Category
$categories = Category::getCategories('xxx');

The issue I have is, that in PHP 8 you cant call non-static functions anymore staticly, but I also can’t rewrite it to $categories = (new Category)->getCategories('xxx'); because its an abstract class.

As a temporary solution, I’ve considered converting getCategories into a static method. However, I’m concerned about potential side effects this change might have on the project.

Does anyone have a suggestion for handling this situation in PHP 8? Ideally, I’m looking for a solution that avoids modifying the method’s static/non-static status while still complying with PHP 8’s stricter standards.

  • 4

    Extend your abstract class and use that one instead

    – 

  • 1

    If the Method is not referencing him self in any way, which seem to be the case, to make method static is the correct way.

    – 

  • You are calling the method in a static way, so it should be declared static, that’s all.

    – 

Leave a Comment