From: brad at info-link dot net Operating system: Linux PHP version: 5.0.0RC1 PHP Bug Type: Feature/Change Request Bug description: Change __sleep to always work on copy of object instead of reference
Description: ------------ When serializing an object with a __sleep method defined, the original object may be modified by __sleep. I'd like to see the __sleep method always being called on a copy of the object being serialized and not on the actual object. In PHP4, this is _mostly_ the case as long as you're not working with references, but in PHP5, the original object is always modified directly. The example below is a little contrived, but does demonstrate the idea. Under PHP4, the result is _almost_ as expected, except that the instance of B referenced by $a->b is modified. If the reference is changed to a plain copy (change the line reading "$a->b = &$b;" to "$a->b = $b;"), then the instance is not modified. PHP5 gives the output given under actual result, which is probably wrong in either case. Reproduce code: --------------- <?php class B { var $b = 'default'; function __sleep() { $this->b = 'serialized'; return Array('b'); } } class A { var $a = "default"; var $b = null; function __sleep() { $this->a = "serialized"; return array_keys(get_object_vars($this)); } } $a = new A(); $b = new B(); $a->b = &$b; print_r($a); $s = serialize($a); print_r($s); print_r($a); $na = unserialize($s); print_r($na); ?> Expected result: ---------------- a Object ( [a] => default [b] => b Object ( [b] => default ) ) O:1:"a":2:{s:1:"a";s:10:"serialized";s:1:"b";O:1:"b":1:{s:1:"b";s:10:"serialized";}}a Object ( [a] => default [b] => b Object ( [b] => default ) ) a Object ( [a] => serialized [b] => b Object ( [b] => serialized ) ) Actual result: -------------- A Object ( [a] => default [b] => B Object ( [b] => default ) ) O:1:"A":2:{s:1:"a";s:10:"serialized";s:1:"b";O:1:"B":1:{s:1:"b";s:10:"serialized";}}A Object ( [a] => serialized [b] => B Object ( [b] => serialized ) ) A Object ( [a] => serialized [b] => B Object ( [b] => serialized ) ) -- Edit bug report at http://bugs.php.net/?id=27785&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=27785&r=trysnapshot4 Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=27785&r=trysnapshot5 Fixed in CVS: http://bugs.php.net/fix.php?id=27785&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=27785&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=27785&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=27785&r=needscript Try newer version: http://bugs.php.net/fix.php?id=27785&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=27785&r=support Expected behavior: http://bugs.php.net/fix.php?id=27785&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=27785&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=27785&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=27785&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=27785&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=27785&r=dst IIS Stability: http://bugs.php.net/fix.php?id=27785&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=27785&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=27785&r=float