note 24669 deleted from language.oop.serialization by danbrown
| From: | danbrown@php.net | Date: | Sat, 07 Nov 2009 19:13:09 +0000 |
| Subject: | note 24669 deleted from language.oop.serialization by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162611@lists.php.net to get a copy of this message | ||
Note Submitter: evan at unixpimps dot org
----
A quick note about objects containing another object as a variable. Say you have something like
this:
class A {
var $A_Var;
}
class B {
var $A
var $B_Var;
function B() {
$this->A = new A();
}
}
$B = new B();
$B->A->A_Var = "A_Var value";
---
When you try to serialize() an instance of B() $B->A->A_Var doesn't seem to keep
it's value.
After much fighting with this the solution I found was to append to my __sleep and __wakeup
functions and serialize $this->A in __sleep and then unserialize it in __wakeup.
class A {
var $A_Var;
}
class B {
var $A
var $A_serialized;
var $B_Var;
function B() {
$this->A = new A();
}
function __sleep() {
$this->A_serialized = serialize($this->A);
return array('A_serialized', 'B_Var');
}
function __wakeup() {
$this->A = unserialize($this->A_serialized);
}
}