php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #40412 __sleep should return private parent members
Submitted: 2007-02-09 09:17 UTC Modified: 2020-12-07 15:00 UTC
Votes:14
Avg. Score:3.9 ± 0.9
Reproduced:13 of 13 (100.0%)
Same Version:0 (0.0%)
Same OS:5 (38.5%)
From: denis at edistar dot com Assigned: cmb (profile)
Status: Closed Package: Variables related
PHP Version: 5.2.1RC4 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: denis at edistar dot com
New email:
PHP Version: OS:

 

 [2007-02-09 09:17 UTC] denis at edistar dot com
Description:
------------
I have two classes: 
- one parent with a private property
- one child that extends the parent

When i serialize the child property I get the following 
error:

Notice: serialize(): "privateProperty" returned as member 
variable from __sleep() but does not exist in test.php on 
line 30

I already know of serializable interface. But the problem 
still persist also using this interface... 
If I define a serialize method in the parent class, 
get_object_vars and reflection api do not return the 
privateProperty value... they continue to think we are 
into the scope of the child class (when we are instead 
into the parent class).





Reproduce code:
---------------
<?

class P {

        private $privateProperty;

        public function __construct() {
                $this->privateProperty = "myValue";
        }


        public function __sleep() {

                return array('privateProperty');
        }

}

class D extends P {

        public function __construct() {
                parent::__construct();
        }

}


$obj = new D();

$s = serialize($obj);
echo $s;


?>


Expected result:
----------------
I expect no warning should be raised and the echo should 
print this:

O:1:"D":1:{s:15:"privateProperty";s:7:"myValue";}



Actual result:
--------------
Notice: serialize(): "privateProperty" returned as member 
variable from __sleep() but does not exist in test.php on 
line 30
O:1:"D":1:{s:15:"privateProperty";N;}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-12-30 23:57 UTC] cmb@php.net
-Package: Feature/Change Request +Package: Variables related
 [2020-12-07 15:00 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2020-12-07 15:00 UTC] cmb@php.net
Use __serialize()[1] instead (Serializable should also work, but
has other known issues).

[1] <https://www.php.net/manual/en/language.oop5.magic.php#object.serialize>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 14:01:29 2024 UTC