Req #40412 [Opn->Csd]: __sleep should return private parent members

From: Date: Mon, 07 Dec 2020 15:00:04 +0000
Subject: Req #40412 [Opn->Csd]: __sleep should return private parent members
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230908@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=40412&edit=1

 ID:                 40412
 Updated by:         cmb@php.net
 Reported by:        denis at edistar dot com
 Summary:            __sleep should return private parent members
-Status:             Open
+Status:             Closed
 Type:               Feature/Change Request
 Package:            Variables related
 Operating System:   Linux
 PHP Version:        5.2.1RC4
-Assigned To:        
+Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

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>


Previous Comments:
------------------------------------------------------------------------
[2007-02-09 09:17:35] 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;}


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=40412&edit=1


Thread (2 messages)

« previous php.bugs (#230908) next »