note 63747 added to language.oop5.magic

From: Date: Wed, 29 Mar 2006 20:46:28 +0000
Subject: note 63747 added to language.oop5.magic
Groups: php.notes 
Request: Send a blank email to php-notes+get-106977@lists.php.net to get a copy of this message
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::"). ---- Server IP: 66.163.161.117 Probable Submitter: 63.214.184.6 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php Edit -- http://master.php.net/note/edit/63747 Del: integrated -- http://master.php.net/note/delete/63747/integrated Del: useless -- http://master.php.net/note/delete/63747/useless Del: bad code -- http://master.php.net/note/delete/63747/bad+code Del: spam -- http://master.php.net/note/delete/63747/spam Del: non-english -- http://master.php.net/note/delete/63747/non-english Del: in docs -- http://master.php.net/note/delete/63747/in+docs Del: other reasons-- http://master.php.net/note/delete/63747 Reject -- http://master.php.net/note/reject/63747 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#106977) next »