Search code examples
phpoopabstraction

How can i enforce an extending class to have a static method?


I basically want class B to implement a method that's been defined in class A. However when i do so i get the following error.

Strict Standards: Static function A::test() should not be abstract in C:\xampp\htdocs\test1.php on line 4

Here is my PHP code:

<?php
error_reporting( E_STRICT );

abstract class A{
    public abstract static function test();
}

class B extends A {
    public  static function test(){
        echo 'Testing';
    };
}

echo B::test();

Solution

  • Static methods are not part of the object, so they shouldn't be extended.

    Make the method concrete.

    I actually struggled with this same problem once I started building unit tests (I almost religiously avoid static methods now, but that's a whole side conversation). Check out this question for someone answering your question berr than I can: Why does PHP 5.2+ disallow abstract static class methods?