#36172 [NEW]: serialize subclasses
From: php at justin dot meagerman dot net
Operating system: linux
PHP version: 5.1.2
PHP Bug Type: Scripting Engine problem
Bug description: serialize subclasses
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 bug report at http://bugs.php.net/?id=36172&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=36172&r=trysnapshot44
Try a CVS snapshot (PHP 5.1): http://bugs.php.net/fix.php?id=36172&r=trysnapshot51
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=36172&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=36172&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=36172&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=36172&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=36172&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=36172&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=36172&r=support
Expected behavior: http://bugs.php.net/fix.php?id=36172&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=36172&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=36172&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=36172&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=36172&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=36172&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=36172&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=36172&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=36172&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=36172&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=36172&r=mysqlcfg
Thread (4 messages)
- php at justin dot meagerman dot net