vintorg
vintorg

Reputation: 217

Can I unserialize an stored object if I added a member to the class definition?

I have a bunch of objects serialized and stored in my database. In the class definition for those objects I added a new member. When I unserialize the objects, will I encounter an error since the serialized objects didn't include that member?

class foo {
  public $alpha = NULL;
}

$myobject = new foo();
$myobject->foo = "This is my original object."

$saved_object = serialize($myobject);
db_save_myobject($saved_object);

After I saved it, I made a change to the foo class...

class foo {
  public $alpha = NULL;
  public $bravo = NULL;
}

Now I want to fetch from my db

$myobject = db_get_myobject();

Will $myobject now have a null bravo member?

Upvotes: 1

Views: 18

Answers (1)

jh1711
jh1711

Reputation: 2328

For simple classes it will work fine.

You'll get $bravo = NULL, unless you assigned something like $myobject->bravo = 123; before serialization. Then it'll stay $bravo = 123.

You may run into trouble if the class or one of its parents implements Serializable or does something funky in __sleep or __wakeup.

Upvotes: 1

Related Questions