note 24669 added to language.oop.serialization
| From: | evan at unixpimps dot org | Date: | Sun, 25 Aug 2002 03:27:00 +0000 |
| Subject: | note 24669 added to language.oop.serialization | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-35565@lists.php.net to get a copy of this message | ||
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);
}
}
--
http://www.php.net/manual/en/language.oop.serialization.php
http://master.php.net/manage/user-notes.php?action=edit+24669
http://master.php.net/manage/user-notes.php?action=delete+24669
http://master.php.net/manage/user-notes.php?action=reject+24669