#34593 [Csd]: Unintuitive serialization for inherited objects.
ID: 34593 User updated by: ectsue at gmail dot com Reported By: ectsue at gmail dot com Status: Closed Bug Type: Feature/Change Request Operating System: * PHP Version: 5.0.* New Comment: helly: Where can I find documentation about the serializable interface? I tried a whole site search on php.net and couldn't find anything of interest. Previous Comments: [2005-09-22 21:10:28] ectsue at gmail dot com tony: In the project that I ran into this problem, I use the __sleep method to return only a subset of the member variables of the class. An object of this class can get to be rather large and I do not need to serialize everything. This class is occasionally used as a superclass for other classes (this is where I ran into the problem). So, I do need that __sleep function. [2005-09-22 10:45:30] [EMAIL PROTECTED] This bug has been fixed in CVS. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. Thank you for the report, and for helping us make PHP better. Have a look at interface serializable in 5.1 [2005-09-22 10:45:30] [EMAIL PROTECTED] This code (with __sleep() removed) works perfecty fine: a; } public function setA($a) { $this->a = $a; } } class B extends A { } $b = new B(); $b->setA(10); $bSerialized = serialize($b); $bUnserialized = unserialize($bSerialized); var_dump($b); var_dump($bUnserialized); ?> Is this what you need? [2005-09-22 07:00:26] ectsue at gmail dot com Description: I believe that the way serialization works for objects doesn't make sense. I have a subclass whose superclass contains a private member variable. Upon serialization, I cannot get the private member variable to serialize for the subclass (except by using a "NUL class-name NUL member-name" string in __sleep()). Imho, subclasses shouldn't have to know what parts of their parent classes to serialize. I can think of two possible solutions to this problem: 1. Have serialize() walk the inheritance tree for the object it is serializing. 2. Have some method that will be able to take the output from parent::__sleep() and modify it so that it can be passed back from the __sleep() method of the subclass so that private member variables in the parent can be serialized. (The function I have in mind would do the NUL class-name NUL member-name transformation). Reproduce code: --- class A { private $a; public function __sleep() { return array('a'); } public function getA() { return $this->a; } public function setA($a) { $this->a = $a; } } class B extends A { } $b = new B(); $b->setA(10); $bSerialized = serialize($b); $bUnserialized = unserialize($bSerialized); var_dump($b); var_dump($bUnserialized); Expected result: object(B)#1 (1) { ["a:private"]=> int(10) } object(B)#1 (1) { ["a:private"]=> int(10) } Actual result: -- object(B)#1 (1) { ["a:private"]=> int(10) } object(B)#2 (2) { ["a:private"]=> NULL ["a"]=> NULL } -- Edit this bug report at http://bugs.php.net/?id=34593&edit=1
#34593 [Csd]: Unintuitive serialization for inherited objects.
ID: 34593 User updated by: ectsue at gmail dot com Reported By: ectsue at gmail dot com Status: Closed Bug Type: Feature/Change Request Operating System: * PHP Version: 5.0.* New Comment: tony: In the project that I ran into this problem, I use the __sleep method to return only a subset of the member variables of the class. An object of this class can get to be rather large and I do not need to serialize everything. This class is occasionally used as a superclass for other classes (this is where I ran into the problem). So, I do need that __sleep function. Previous Comments: [2005-09-22 10:45:30] [EMAIL PROTECTED] This bug has been fixed in CVS. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. Thank you for the report, and for helping us make PHP better. Have a look at interface serializable in 5.1 [2005-09-22 10:45:30] [EMAIL PROTECTED] This code (with __sleep() removed) works perfecty fine: a; } public function setA($a) { $this->a = $a; } } class B extends A { } $b = new B(); $b->setA(10); $bSerialized = serialize($b); $bUnserialized = unserialize($bSerialized); var_dump($b); var_dump($bUnserialized); ?> Is this what you need? [2005-09-22 07:00:26] ectsue at gmail dot com Description: I believe that the way serialization works for objects doesn't make sense. I have a subclass whose superclass contains a private member variable. Upon serialization, I cannot get the private member variable to serialize for the subclass (except by using a "NUL class-name NUL member-name" string in __sleep()). Imho, subclasses shouldn't have to know what parts of their parent classes to serialize. I can think of two possible solutions to this problem: 1. Have serialize() walk the inheritance tree for the object it is serializing. 2. Have some method that will be able to take the output from parent::__sleep() and modify it so that it can be passed back from the __sleep() method of the subclass so that private member variables in the parent can be serialized. (The function I have in mind would do the NUL class-name NUL member-name transformation). Reproduce code: --- class A { private $a; public function __sleep() { return array('a'); } public function getA() { return $this->a; } public function setA($a) { $this->a = $a; } } class B extends A { } $b = new B(); $b->setA(10); $bSerialized = serialize($b); $bUnserialized = unserialize($bSerialized); var_dump($b); var_dump($bUnserialized); Expected result: object(B)#1 (1) { ["a:private"]=> int(10) } object(B)#1 (1) { ["a:private"]=> int(10) } Actual result: -- object(B)#1 (1) { ["a:private"]=> int(10) } object(B)#2 (2) { ["a:private"]=> NULL ["a"]=> NULL } -- Edit this bug report at http://bugs.php.net/?id=34593&edit=1
#34593 [NEW]: Unintuitive serialization for inherited objects.
From: ectsue at gmail dot com Operating system: MacOS X PHP version: 5.0.5 PHP Bug Type: Feature/Change Request Bug description: Unintuitive serialization for inherited objects. Description: I believe that the way serialization works for objects doesn't make sense. I have a subclass whose superclass contains a private member variable. Upon serialization, I cannot get the private member variable to serialize for the subclass (except by using a "NUL class-name NUL member-name" string in __sleep()). Imho, subclasses shouldn't have to know what parts of their parent classes to serialize. I can think of two possible solutions to this problem: 1. Have serialize() walk the inheritance tree for the object it is serializing. 2. Have some method that will be able to take the output from parent::__sleep() and modify it so that it can be passed back from the __sleep() method of the subclass so that private member variables in the parent can be serialized. (The function I have in mind would do the NUL class-name NUL member-name transformation). Reproduce code: --- class A { private $a; public function __sleep() { return array('a'); } public function getA() { return $this->a; } public function setA($a) { $this->a = $a; } } class B extends A { } $b = new B(); $b->setA(10); $bSerialized = serialize($b); $bUnserialized = unserialize($bSerialized); var_dump($b); var_dump($bUnserialized); Expected result: object(B)#1 (1) { ["a:private"]=> int(10) } object(B)#1 (1) { ["a:private"]=> int(10) } Actual result: -- object(B)#1 (1) { ["a:private"]=> int(10) } object(B)#2 (2) { ["a:private"]=> NULL ["a"]=> NULL } -- Edit bug report at http://bugs.php.net/?id=34593&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=34593&r=trysnapshot4 Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=34593&r=trysnapshot50 Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=34593&r=trysnapshot51 Fixed in CVS:http://bugs.php.net/fix.php?id=34593&r=fixedcvs Fixed in release:http://bugs.php.net/fix.php?id=34593&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=34593&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=34593&r=needscript Try newer version: http://bugs.php.net/fix.php?id=34593&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=34593&r=support Expected behavior: http://bugs.php.net/fix.php?id=34593&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=34593&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=34593&r=submittedtwice register_globals:http://bugs.php.net/fix.php?id=34593&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=34593&r=php3 Daylight Savings:http://bugs.php.net/fix.php?id=34593&r=dst IIS Stability: http://bugs.php.net/fix.php?id=34593&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=34593&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=34593&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=34593&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=34593&r=mysqlcfg