I want to do some cache in my ORM
class Base {
static public $v=array();
static public function createById($id){
if(!array_key_exists($id, static::$v)){
static::$v[$id] = new static; //Get from DB here. new static is just example
}
return static::$v[$id];
}
}
class User extends Base{
}
class Entity extends Base{
}
But now cache is merged
var_dump(User::createById(1));
var_dump(Entity::createById(1));
results
object(Model\User)#4 (0) {
}
object(model\User)#4 (0) {
}
If I made
class Entity extends Base{
static public $v=array();
}
class User extends Base{
static public $v=array();
}
I get what I need:
object(Model\User)#4 (0) {
}
object(model\Entity)#5 (0) {
}
Is it possible to do it without declaration in every class?
If its that important that you don’t re-declare the property in each child class, the only solution I can think of is, which isn’t exactly what you wanted, but it should get you the same functionality, is sharing the same property on the base class to store the cache for all the child classes, but using the child class name as a key in the cache array:
Yes, its not pretty… but AFAIK, what you asked for isn’t possible.