Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strict standards: Static function model::tableStruct() should not be abstract in

Tags:

php

class

static

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?

like image 763
Alex Avatar asked Nov 21 '12 13:11

Alex


2 Answers

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();
}
like image 61
Brent Baisley Avatar answered Sep 28 '22 06:09

Brent Baisley


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!!
    }
}
like image 37
Noener Avatar answered Sep 28 '22 06:09

Noener