static properties are inherited, if you want to have them separately, redefine them
class A extend Model { static $cache; }
class B extend Model { static $cache; }
also what you are trying to do is bad OOP practice.
this is how you can do it proper way
class CacheFactory
{
static public function createModelCache($model)
{
static $instances = [];
if (!isset($instances[$model])) {
$instances[$model] = new Cache($model);
}
return $instances[$model];
}
}
class Cache
{
protected $name;
protected $data = [];
public function __construct($name)
{
$this->name = $name;
}
public function get($key)
{
return isset($this->data[$key]) ? $this->data[$key] : null;
}
public function set($key, $value)
{
$this->data[$key] = $value;
}
}
abstract class Model
{
abstract protected $name;
/**
* @type Cache
*/
protected $cache;
public function __construct()
{
$this->cache = CacheFactory::createModelCache($this->name);
}
public function findById($id)
{
if (($data = $this->cache->get($id)) !== null) {
return $data;
}
$data = ...;
$this->cache->put($id, $data);
}
}
class A extends Model
{
protected $name = "modelA";
}
class B extends Model
{
protected $name = "modelB";
}
this just an example and you can change few bits like
- you can move
CacheFactory::createModelCache to Cache
- you can use
get_class instead using abstract protected $name
- you can just create static array instead
Cache class
but thing is - you just don't use static everywhere, this is not how you code in OOP
from my example you can see the only thing what is static is an array with Cache objects and factory method. this is only thing you need to store globally.