Is there a way to deserialize an object into "$this"?
Posted
by Andreas Bonini
on Stack Overflow
See other posts from Stack Overflow
or by Andreas Bonini
Published on 2010-06-01T20:04:07Z
Indexed on
2010/06/01
20:13 UTC
Read the original article
Hit count: 185
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?
© Stack Overflow or respective owner