6

This message shows in php 5.4 for some weird reason.

My class looks like this:

abstract class model{

  private static
    $tableStruct = array();

  abstract protected static function tableStruct();

  public static function foo(){
    if(!isset(self::$tableStruct[get_called_class()]))
      self::$tableStruct[get_called_class()] = static::tableStruct();

    // I'm using it here!!
  }

}

and should be used like:

class page extends model{

  protected static function tableStruct(){
    return array(
      'id' => ...
      'title' => ...
    );
  }

  ...

}

Why making a static method required by child classes is considered to be against the standards?

Alex
  • 66,732
  • 177
  • 439
  • 641
  • It's not just 5.4, its been like that for a while. See [similar question](http://stackoverflow.com/questions/999066/why-does-php-5-2-disallow-abstract-static-class-methods). – Brent Baisley Nov 21 '12 at 14:03

2 Answers2

7

Abstract static methods are kind of an odd concept. A static method basically "hard codes" the method to the class, making sure there is only a single instance (~singleton). But making it abstract means you want to force some other class to implement it.

I see what you are trying to do, but when dealing with abstract classes, I would avoid static methods in the base class. What you can do instead is use late static binding (static::) to call a tableStruct method in the "child" class. This doesn't force the the method to be implemented like abstract does, but you could test for the implementation and throw an exception if it doesn't exist.

public static function foo(){
    // call the method in the child class 
    $x = static::tableStruct();
}
Brent Baisley
  • 12,641
  • 2
  • 26
  • 39
  • "...when dealing with abstract classes, I would avoid [abstract] static methods in the base class." Could you please give a practical example where using an abstract static method will hurt? – user151851 Mar 06 '14 at 17:31
  • 1
    static references create tight coupling. static methods in general make testing difficult because it's hard to replace the class with a mock object. – Brent Baisley Mar 07 '14 at 04:28
3

For what it's worth...

Abusing interfaces:

interface Imodel {

    static function tableStruct();        
}

abstract class model implements Imodel {

    private static $tableStruct = array();

    public static function foo() {
        if (!isset(self::$tableStruct[get_called_class()]))
            self::$tableStruct[get_called_class()] = static::tableStruct();

        // I'm using it here!!
    }
}
Codium
  • 3,200
  • 6
  • 34
  • 60
Noener
  • 39
  • 1
  • Normally this is the pattern i would support, but the original post specifies the desired method in the child class is `protected`, and Interfaces don't allow that. i'm trying to do something similar to the OP, and it seems there's no "elegant" answer that complies with standards. Either an interface that requires `public` methods, or an abstract-level empty method (or a method-exists check, icky) that fills in for missing implementations. Yuck for both. – cautionbug Jul 02 '18 at 19:46