Nathan Nobbe wrote:
all, i was playing around w/ some object serialization tonight during further exploration of spl and i stumbled on what appears to be a bug in the behavior of the __sleep() magic method. here is the pertinent documentation on the method ..is supposed to return an array with the names of all variables of that object that should be serialized. so, the idea is, *only* the instance variables identified in the array returned are marked for serialization. however, it appears all instance variables are being serialized no matter what. see the reproducible code below. ive run this on 2 separate php5 boxes, one w/ 5.2.5, another w/ a 5.2.something.. <?php class A { public $a1 = 'a1'; public $a2 = 'a2'; public $a3 = 'a3'; public function __sleep() { echo __FUNCTION__ . PHP_EOL; return array('a1', 'a2'); } } var_dump(unserialize(serialize(new A()))); ?> this is what i get despite having marked only member variables 'a', and 'b' for serialization. __sleep object(A)#1 (3) { ["a1"]=> string(2) "a1" ["a2"]=> string(2) "a2" ["a3"]=> string(2) "a3" } consensus ?
To check if __sleep is proper, you should be doing var_dump(serialize(new A())); unserialize'ing effectively also does a __wakeup() This should give a clearer picture <?php class A { public $a1 = 'a1'; public $a2 = 'a2'; public $a3 = null; public function __construct(){ $this->a3 = 'a3'; } public function __sleep() { echo __FUNCTION__ . PHP_EOL; return array('a1', 'a2'); } } var_dump(unserialize(serialize(new A()))); ?> __sleep object(A)#1 (3) { ["a1"]=> string(2) "a1" ["a2"]=> string(2) "a2" ["a3"]=> NULL } ============= and ====================== <?php class A { public $a1 = 'a1'; public $a2 = 'a2'; public $a3 = null; public function __construct(){ $this->a3 = 'a3'; } public function __sleep() { echo __FUNCTION__ . PHP_EOL; return array('a1', 'a2', 'a3'); } } var_dump(unserialize(serialize(new A()))); ?> __sleep object(A)#1 (3) { ["a1"]=> string(2) "a1" ["a2"]=> string(2) "a2" ["a3"]=> string(2) "a3" } -- Regards, Anup Shukla -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php