note 63747 deleted from language.oop5.magic by bjori
| From: | bjori@php.net | Date: | Thu, 30 Mar 2006 10:12:07 +0000 |
| Subject: | note 63747 deleted from language.oop5.magic by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-107007@lists.php.net to get a copy of this message | ||
Note Submitter: nitrous at fuckoff dot com
Reason: bad code
----
Inheritence and private members:
Be careful with __sleep() return values. If you are serializing a child and using a parent with
private members... it's very difficult to specify the parent's private variables. In fact,
I think it's not possible (PHP 5.1).
The work around is to use array_keys(), since it returns a special object, not a simple array. You
can merge the results with array_merge just fine. Be careful... as member variable lists (with this
method) don't hold simple scalar values.
<?
// DO THIS FUNCTION
function A::__sleep() {
return array_keys((array) $this, array(
'a_public', 'a_protected', 'a_private'
));
}
function A::__sleep() {
return array(
'a_public', 'a_protected', 'a_private'
);
}
?>
Then, this will work for private members:
<?
function B::__sleep() {
return array_merge(
parent::__sleep(),
array_keys((array) $this, array('b_public', 'b_protected',
'b_private'))
);
}
?>
This is pseudo-code. The scope was added for clarity ("A::" and "B::").