On Tue, Jul 31, 2001 at 02:48:48PM -0600, mike cullerton wrote:
> on 7/31/01 1:48 PM, scott [gts] at [EMAIL PROTECTED] wrote:
> 
> > I am having a problem with __sleep();
> > there mere existance of it is causing my object
> > to not get serialized at all.  __wakeup() works fine.
> > 
> > i am using PHP v4.0.6 / apache / win2k.
> > 
> > If i keep __sleep() in the object, it will not serialize,
> > but if i remove it, it serialized fine.  Does anyone
> > know why this happens?
> 
> i asked this about a month ago and didn't hear anything. i too have never
> been able to get __sleep to work. i think it's because i couldn't figure out
> what needed to be returned (or how to return it :)
> 
> at http://www.php.net/manual/en/language.oop.magic-functions.php it says
> that __sleep is "supposed to return an array with the names of all variables
> of that object that should be serialized", but there are no examples of
> this.
> 
> in a current project, i have an object that is registed as a session
> variable. it contains three objects and an array. one of the objects is a
> PEAR db object, and i don't need to serialize it. i do want to maintain the
> other two objects and the array.
> 
> i tried a number of ideas inside __sleep, to no avail. without __sleep, it
> works. with __sleep, i break it.
> 
> i do use __wakeup to reinitialize my db object, but i just use
> $db->disconnect(); at the end of my index file to disconnect .
> 
> does anyone know the proper way to "clean up the object" in __sleep and how
> to return the variables that should be serialized? can this even be used
> when one (or more) of the variables is an object itself?
> 

    see sample - it should be self-explaining.

    tc

<?
error_reporting(-1);
class test {
        var $filename;
        var $mode;
        var $fd;

        function test($filename=NULL,$mode=NULL) {
                echo "------------------constructor called\n";
                $this->filename = $filename;
                $this->mode = $mode;

                // we could call $this->_wakeup() instead!
                if ($this->filename && $this->mode) {
                        $this->fd = fopen($this->filename,$this->mode);
                }

        }

        function _sleep() {
                echo "------------------sleep\n";
                // return list of instance-variables to be serialized
                return array("filename","mode");
        }            

        function _wakeup() {
                echo "------------------wakeup\n";       
                // all serialized instance variables are set now, inititalize the 
non-serializeable ones
                if ($this->filename && $this->mode) {            
                        $this->fd = fopen($this->filename,$this->mode);
                }
        }
}

$a = new test("/tmp/thies.tcsh","r");
echo $a;            
var_dump($a);
$b = serialize($a);
var_dump($b);
$a = unserialize($b);
var_dump($a);

$b = wddx_serialize_value($a);
var_dump($b);
$a = wddx_deserialize($b);
var_dump($a);

?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to