Hi,
I need to bea be able to use a static variable set in a class that extends a base class... from the base class.
Consider this:
class Animal {
public static $color = 'black';
public static function get_color()
{
return self::$color;
}
}
class Dog extends Animal {
public static $color = 'brown';
}
echo Animal::get_color(); // prints 'black'
echo Dog::get_color(); // also prints 'black'
This works wonderfully in PHP 5.3.x (Dog::get_color()
prints 'brown') since it has late static binding. But my production server runs PHP 5.2.11 and so I need to adapt my script.
Is there a somewhat pretty workaround to solve this issue?
Cheers!
Christoffer
EDIT: The goal
As noted below, this is a very much simplified example of what I am trying to accomplish. If I provide you with the two options I have used to solve my problem (and the problem itself) someone might have a different solution than I...
I have built a base database model that contains functions like "find", "find_by" and "find_all" (all static).
In PHP 5.3 there is a function called get_called_class()
which I currently use to determine the called class's name, and then use it to map against the correct database table. Ex class User
would point to users
.
get_called_class()
doesn't exist in PHP 5.2.x and the hack implementations I've found are very unreliable. Then I turned to this option of using a static variable in all model classes which contain the class name.