I'm writing a class to handle a memcached object. The idea was to create abstract class Cachable
and all the cachable objects (such as User, Post, etc) would be subclasses of said class.
The class offers some method such as Load()
which calls the abstract function LoadFromDB()
if the object is not cached, functions to refresh/invalidate the cache, etc.
The main problem is in Load()
; I wanted to do something similar:
protected function Load($id)
{
$this->memcacheId = $id;
$this->Connect();
$cached = $this->memcache->get(get_class($this) . ':' . $id);
if($cached === false)
{
$this->SetLoaded(LoadFromDB($id));
UpdateCache();
} else {
$this = $cached;
$this->SetLoaded(true);
}
}
Unfortunately I need $this
to become $cached
(the cached object); is there any way to do that? Was the "every cachable object derives from the cachable class" a bad design idea?