php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #36172 subclass serialization of parent private members
Submitted: 2006-01-26 19:42 UTC Modified: 2010-01-10 13:16 UTC
Votes:5
Avg. Score:3.8 ± 1.0
Reproduced:3 of 4 (75.0%)
Same Version:1 (33.3%)
Same OS:1 (33.3%)
From: php at justin dot meagerman dot net Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.1.2 OS: linux
Private report: No CVE-ID: None
 [2006-01-26 19:42 UTC] 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] => 
)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-26 20:24 UTC] helly@php.net
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
 [2006-01-26 21:35 UTC] php at justin dot meagerman dot net
Thank you for the workaround. However, the existence of a workaround does not invalidate the bug. This is clearly not expected behavior. Moreover, when __sleep methods are not defined, the behaviour is as expected, as noted below.

If this really is not a bug, then it needs to be documented in the __sleep documentation page (and this bug's category should be changed to reflect that). 


<?php
class A {
  private $a;

  public function __construct() {
    $this->a = 'aVal';
  }
}

class B extends A {
  private $b;

  public function __construct() {
    parent::__construct();
    $this->b = 'bVal';
  }
}

$serialB = serialize(new B());
print_r(unserialize($serialB));
?>

outputs:

B Object
(
    [b:private] => bVal
    [a:private] => aVal
)
 [2006-01-27 08:38 UTC] sniper@php.net
reclassified.
 [2006-01-30 15:59 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"Method does not have access to its parent's class private properties. It is possible to serialize them through SPL  interface Serializable."
 [2008-04-16 13:30 UTC] cweiske@php.net
The docs for __sleep still do not mention anything
 [2010-01-10 13:02 UTC] svn@php.net
Automatic comment from SVN on behalf of degeberg
Revision: http://svn.php.net/viewvc/?view=revision&revision=293345
Log: Fixed PHP bug #36172 (subclass serialization of parent private members).
 [2010-01-10 13:16 UTC] degeberg@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC