0

I have this system which I'm working on:

abstract class Model {
    static $table = "";
    abstract static function init();

    public static function getById() {
        $table = self::$table;
    }
}

class Model_user extends Model {
    static function init() {
        self::$table = "users";
    }
}

class Model_post extends Model {
    static function init() { self::$table = "post"; }
}

// ...

Model_user::init();
Model_post::init();
$user = Model_user::getById(10);
$post = Model_user::getById(40);

I want it to be so each subclass has its own set of static members which can be accessed by the static functions in Model. I can't use the static:: keyword because I have to use PHP 5.2.16. Unfortunately, I can't just say "self::" because of a problem with PHP revealed in the below example:

class Foo {
    static $name = "Foo";

    static function printName() {
        echo self::$name;
    }
}

class Bar extends Foo {
    static $name = "Bar";
}

class Foobar extends Foo {
    static $name = "Foobar";
}

Bar::printName();
echo "<br />";
Foobar::printName();

Which displays:

Foo<br />Foo

When it should display:

Bar<br />Foobar

Any way this could be done?

Matt Eskridge
  • 1,019
  • 10
  • 24

2 Answers2

1

It seems you cannot access the children classes static members in the parent's static method's code. A solution has been posted in this comment on the php documentation about the static keyword. The solution would be to make your table variable an array of this form:

$table = array ('classname' => 'tablename', 'secondClassname' => 'secondTablename');
greg0ire
  • 22,714
  • 16
  • 72
  • 101
  • I redesigned my class to be non-abstract and take a type parameter, which is a key to an array containing the data about the model, such as the table structure. I guess it won't be as pretty, but oh well. Thanks. – Matt Eskridge Apr 17 '11 at 19:22
0

Everything that is static belongs to the class, not to an object. Therefore static methods are not inherited from a parent class and will only access members inside the class they are defined in.

In Java this sort of static method call (Foobar::printName()) would even throw an error.

halfdan
  • 33,545
  • 8
  • 78
  • 87
  • How come php does not even give a strict standards error then? What do you mean exactly by "not inherited"? – greg0ire Apr 17 '11 at 18:38
  • I see. then how should one implement a system like the one I mentioned where each subclass of model can have static factory functions which use a static table variable specific to the subclass? – Matt Eskridge Apr 17 '11 at 18:41
  • @Cosmic, you can't implement a system like this. - @greg0ire, because PHP doesn't care? static methods do belong to the **class**, normal methods are inherited – halfdan Apr 17 '11 at 19:00
  • Then any idea how, say, rails does this? In rails most of the functionality of Models are in the superclass but you create a subclass which extends model that holds all the functionality, including static functionality, of the superclass. – Matt Eskridge Apr 17 '11 at 19:04
  • @halfdan : I can't see the incompatibility between "class belonging" and inheritance. Look at the case of the `Animal` class : it has a static method `getCount()` . Why couldn't the class `Dog` inherit this static method? It makes perfectly sense to me. – greg0ire Apr 17 '11 at 19:17
  • greg0ire, sorry but I'm not here to tell you the basics of OOP. Have a look at other posts like this one: http://stackoverflow.com/questions/3284/why-cant-i-have-abstract-static-methods-in-c – halfdan Apr 17 '11 at 19:34
  • @halfdan : why are you pointing me towards a post about .net ? Are you saying that OOP has a definition somewhere? I think each language has its own definition, do they not? – greg0ire Apr 17 '11 at 19:47
  • *sigh* No greg0ire, there are differences between languages. But it's just fact that a static member does ONLY belong to the class and methods belong to the object of a class. Everything that belongs to an object gets inherited into subclasses. getCount() would not be static. It's not a matter of "sense" but a matter of what is fact. Live with it. – halfdan Apr 17 '11 at 20:12
  • I think you misunderstood me. `getCount()` would represent the number of instances of this class, and it would be `static`. Because if has nothing to do with the object itself (a human) but with the group of all instances of this class (humanity). But maybe you understood `getCount()` as something else... You seem to think I don't know that static methods don't need an instanciated object to be called. I can't understand what gave you this impression. And private members or methods do not get inherited in subclasses. – greg0ire Apr 17 '11 at 22:25