Mike Nicewarner
Mike Nicewarner

Reputation: 61

In PHP, how can I unserialize into the current object?

This is probably a noob question, so please be kind.

I'm trying to implement a cache on an expensive "activity" object. In the constructor I first check the cache to see if this Activity instance already exists. If not, I do all the queries to build up the object, serialize it and save it to cache. The next time I come in, I check the cache and my object is there, so I unserialize it. Now is my problem, how do I put that object into $this, the current object? I can't just say "$this = unserialize($row[0]);" That fails with the error message, "Cannot re-assign $this in ActivityClass.php". What am I missing?

Thanks a ton! Mike

Upvotes: 6

Views: 2562

Answers (3)

Kromey
Kromey

Reputation: 1212

You'll have to serialize only your object's internal state, i.e. its parameters (aka "member variables"). In fact, in this instance, serialize() isn't really what you want to do; rather, you want to store your ActivityClass's data to your cache, not the serialization of the entire object. This gets tricky, though, because as you add new parameters later you need to remember to store these in your cache as well.

Alternatively, you can implement a singleton or factory pattern for your ActivityClass. Since you say you're pulling the class from the cache in the constructor, I take it that only one instance of this class is meant to exist at any given time? In this case, you should make your class a singleton, by doing the following:

  1. Make the __construct() method private or protected.
  2. Create a public static method (I tend to call this getInstance()) that will check your cache for the object, or instantiate a new one and then cache it.

Now instead of directly instantiating a new ActivityClass object, you instead write $foo = ActivityClass::getInstance();, which gives you either a new object or unserializes and returns your cached one.

Upvotes: 1

Tesserex
Tesserex

Reputation: 17314

If you don't want your construction to leave the class, you can create a factory method:

class Activity
{
    public static function Create(/* your params */)
    {
        // construct cache and key, whatever
        $obj = unserialize($cache->get($key));

        if ($obj) return $obj;

        return new Activity(/* params */);
    }

    // rest of your stuff
}

Upvotes: 3

Pascal MARTIN
Pascal MARTIN

Reputation: 401032

As you noticed, you cannot just override the current object as a whole.


Instead, a possibility would be to store the data you're serializing/unserializing into a property of your object.

This way, you wouldn't serialize your whole object, but only one of its properties -- and only that single property would be overriden when unserializing.

Typically, you wouldn't serialize the connection to the database, which could be another property of your object.


Another possibility would be to not have your object deal with its own (de-)serialization.

Instead, you should :

  • Use an external class to instanciate your object
  • With that external class being responsible of either :
    • Loading data from cache and pushing it into your object,
    • Or calling the right method of your class, to load data from the database -- and, then, save that object to cache.

Upvotes: 0

Related Questions