Gustav Wiberg wrote:
Hi again!
I really can't find a manual for __sleep on the php.net site!? But I understand that __sleep is called before serialize and __wakeup is called before unserialize?
My bad, it's __wakeup not __wake. But still, a quick search of php.net
for __sleep found this...
http://php.net/oop.magic-functions
Do you mean that I create a function __sleep in my class where I serialize my object and create a function __wakeup in my class where i unserialize my object?
No. Read that manual page because it explains it quite well. You
implement __sleep and __wakeup in your class. Then, when you call
serialize it calls __sleep before serializing that object, and when you
call unserialize it calls __wakeup.
You *do not* call serialize and unserialize inside your class to do
anything to an instance of that class (well, you can but that just
complicates the point).
I was looking for a way to achieve this: (This maybe isn't possible?)
1. Save the serialized object into the database.
2. Load the serialized object from database and unserialize and USE it.
(The problem is to USE it).
Simple. Say you have $obj which is an instance of the Widget class.
$obj_serialized = serialize($obj);
mysql_query('insert into ... set obj =
"'.mysql_real_escape_string($obj_serialized).'" where ...');
Then to read it simply do the select to get the serialized data. Let's
say you put it into a variable called $obj_serialized.
$obj = unserialize($obj_serialized);
You can now use $obj in exactly the same way as you ever would.
$obj->Method('apples', 'pears', 'me', 'old', 'china');
I apologise for confusing things by mentioning sleep and wakeup. These
methods allow you to write code that will get executed when an instance
of your class gets serialized or unserialized. For most classes this
will not be needed.
However, say for example you were writing a DB abstraction class. Since
a DB connection is a resource it will not be stored when serialized.
Therefore you can use an __sleep method to gracefully disconnect the
object from the database when serialized, and __wakeup to reconnect it
when unserialized.
I hope that makes a bit more sense.
-Stut
--
http://stut.net/
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php