#36172 [Opn->Bgs]: subclass serialization of parent private members

From: Date: Thu, 26 Jan 2006 19:24:43 +0000
Subject: #36172 [Opn->Bgs]: subclass serialization of parent private members
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-92523@lists.php.net to get a copy of this message
ID: 36172 Updated by: helly@php.net Reported By: php at justin dot meagerman dot net -Status: Open +Status: Bogus Bug Type: Scripting Engine problem Operating System: linux PHP Version: 5.1.2 New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Use interface Serializable, see: $> php --rc Serializable Previous Comments: ------------------------------------------------------------------------ [2006-01-26 19:48:36] php at justin dot meagerman dot net this is a better title ------------------------------------------------------------------------ [2006-01-26 19:42:57] php at justin dot meagerman dot net Description: ------------ With a private member defined in a parent class and a subclass which defines a __sleep method, serialization of instances of the subclass will not save the parent's private member. This works as expected if __sleep methods are not defined. Clearly, because of name conflicts, the child __sleep cannot return the names of the parent's private members (directly, or indirectly by calling parent::__sleep), and therefore I think PHP might need to call __sleep for each class in the hierarchy. This may be the same issue the no-feedback bug #35779 was trying to convey. Reproduce code: --------------- class A { private $a; public function __construct() { $this->a = 'aVal'; } public function __sleep() { return array('a'); } } class B extends A { private $b; public function __construct() { parent::__construct(); $this->b = 'bVal'; } public function __sleep() { //return array('b'); return array_merge(parent::__sleep(), array('b')); } } $serialB = serialize(new B()); print_r(unserialize($serialB)); Expected result: ---------------- B Object ( [b:private] => bVal [a:private] => aVal ) Actual result: -------------- Notice: serialize(): "a" returned as member variable from __sleep() but does not exist in [...] B Object ( [b:private] => bVal [a:private] => [a] => ) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=36172&edit=1

« previous php.bugs (#92523) next »