#34593 [Opn->Fbk]: Unintuitive serialization for inherited objects.

From: Date: Thu, 22 Sep 2005 08:45:36 +0000
Subject: #34593 [Opn->Fbk]: Unintuitive serialization for inherited objects.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-85418@lists.php.net to get a copy of this message
ID: 34593 Updated by: tony2001@php.net Reported By: ectsue at gmail dot com -Status: Open +Status: Feedback Bug Type: Feature/Change Request Operating System: MacOS X PHP Version: 5.0.5 New Comment: This code (with __sleep() removed) works perfecty fine: <?php class A { private $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); ?> Is this what you need? Previous Comments: ------------------------------------------------------------------------ [2005-09-22 10:45:30] tony2001@php.net This code (with __sleep() removed) works perfecty fine: <?php class A { private $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); ?> 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

« previous php.bugs (#85418) next »