RE: [PHP] Re: issues with __sleep() and __wakeup()

From: Date: Wed, 01 Aug 2001 15:32:28 +0000
Subject: RE: [PHP] Re: issues with __sleep() and __wakeup()
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-60677@lists.php.net to get a copy of this message
i added a (hopefully helpfull) addition to the online notes regarding magic-functions: http://www.php.net/manual/en/language.oop.magic-functions.php Here's what i posted, if anyone's interested: Here is a sample class and some test statements to demonstrate how __sleep() is used. If you do not return anything from __sleep(), your object will *not* be serialized. You must return something from __sleep() to tell serialize what to serialize. <? // to test the class $x = new Scott(); print_r($x); $y = serialize($x); $z = unserialize($y); print_r($z); // Simple class to test __sleep() class Scott { // class variables var $error; var $svar = array(); // constructor function Scott() { $this->svar['Hello'] = "World"; } function __sleep() { $this->svar['Hello'] = "Yawn"; // return list of instance-variables to be serialized return array('error', 'svar'); } function __wakeup() { $this->svar['test'] = "I'm here!"; } }// end class ?> > -----Original Message----- > From: Richard Lynch [mailto:ceo@l-i-e.com] > Sent: Wednesday, August 01, 2001 3:52 AM > To: php-general@lists.php.net > Subject: [PHP] Re: issues with __sleep() and __wakeup() > > > > class Scott { > > var $svar = array(); // free-form hash for whatever data > > function Scott( ) > > { > > return $this; > > Like, I don't think you're supposed to return $this from your constructor... > > > } > > function __sleep() > > { > > According to the manual, this is supposed to return an array of the variable > names you want serialized. > > return(array('svar')); > > in this case, I think, would be what you want... > > > } > > function __wakeup() > > { > > $this->svar['sleep'] = "I am waking up"; > > } > > }// end class > > Once you get it working, post your sample to the User Contributed notes > please :-) > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#60677) next »