note 76643 added to language.oop5.magic
| From: | amir_abiri at ipcmedia dot com | Date: | Tue, 24 Jul 2007 11:58:31 +0000 |
| Subject: | note 76643 added to language.oop5.magic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-128882@lists.php.net to get a copy of this message | ||
Another small thing that is important to note about __sleep() and privte member variables:
<?php
class A
{
private $a;
public function __construct()
{
$this->a = 1;
}
}
class B extends A
{
protected $b;
public function __construct()
{
parent::__construct();
$this->b = 2;
}
function __sleep()
{
return array('a', 'b');
}
}
serialize(new B);
?>
result:
Notice: serialize(): "a" returned as member variable from __sleep() but does not exist in
...
To summerize: in a given class hierarchy in which parent classes contain private member variables,
those variables are serialized when __sleep() is not defined. However, once __sleep() is defined,
there is no way to make those private member variables serialized as well. From that point on,
serialization is performed from the visibility scope of the subclass.
It is particularly important to note this little quirk when designing base classes that their
derivables may be serialized, or when subclassing an external library class.
----
Server IP: 69.147.83.197
Probable Submitter: 64.236.80.62
----
Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php
Edit -- https://master.php.net/note/edit/76643
Del: integrated -- https://master.php.net/note/delete/76643/integrated
Del: useless -- https://master.php.net/note/delete/76643/useless
Del: bad code -- https://master.php.net/note/delete/76643/bad+code
Del: spam -- https://master.php.net/note/delete/76643/spam
Del: non-english -- https://master.php.net/note/delete/76643/non-english
Del: in docs -- https://master.php.net/note/delete/76643/in+docs
Del: other reasons-- https://master.php.net/note/delete/76643
Reject -- https://master.php.net/note/reject/76643
Search -- https://master.php.net/manage/user-notes.php