note 59560 added to language.oop5.magic
| From: | docey at osu1 dot php dot net | Date: | Fri, 09 Dec 2005 16:44:36 +0000 |
| Subject: | note 59560 added to language.oop5.magic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-99855@lists.php.net to get a copy of this message | ||
about __sleep and _wakeup, consider using a method like this:
class core
{
var $sub_core; //ref of subcore
var $_sleep_subcore; // place where serialize version of sub_core will be stored
function core(){
$this->sub_core = new sub_core();
return true;
}
function __wakeup()
{
// on wakeup of core, core unserializes sub_core
// wich it had stored when it was serialized itself
$this->sub_core = unserialize($this->_sleep_subcore);
return true;
}
function __sleep()
{
// sub_core will be serialized when core is serialized.
// the serialized subcore will be stored as a string inside core.
$this->_sleep_subcore = serialize($this->sub_core);
$return_arr[] = "_sleep_subcore";
return $return_arr;
}
}
class sub_core
{
var $info;
function sub_core()
{
$this->info["somedata"] = "somedata overhere"
}
function __wakeup()
{
return true;
}
function __sleep()
{
$return_arr[] = "info"
return $return_arr;
}
}
this way subcore is being serialized by core when core is being serialized. subcore handles its own
data and core stores it as a serialize string inside itself. on wakeup core unserializes subcore.
this may have a performance cost, but if you have many objects connected this way this is the best
way of serializing them. you only need to serialize the the main object wich will serialize all
those below which will serialize all those below them again. in effect causing a sort of
chainreaction in wich each object takes care of its own info.
offcoarse you always need to store the eventualy serialized string in a safe place. somebody got
experience with this way of __wakeup and __sleep.
works in PHP4&5
----
Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+59560
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59560&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59560&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59560&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59560&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59560&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59560&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59560&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+59560&report=yes
Search -- http://master.php.net/manage/user-notes.php