note 81492 added to language.oop5.magic

From: Date: Sat, 01 Mar 2008 02:22:27 +0000
Subject: note 81492 added to language.oop5.magic
Groups: php.notes 
Request: Send a blank email to php-notes+get-136625@lists.php.net to get a copy of this message
The above hint for using array_keys((array)$obj) got me investigating how to get __sleep to really work with object hierarchies. With PHP 5.2.3, If you want to serialize an object that is part of an object hierarchy and you want to selectively serialize members (public, private, and protected) by manually specifying the array of members, there are a few simple rules for naming members that you must follow: 1. public members should be named using just their member name, like so: <?php class Foo { public $bar; public function __sleep() { return array("bar"); } } ?> 2. protected members should be named using "\0" . "*" . "\0" . member name, like so: <?php class Foo { protected $bar; public function __sleep() { return array("\0*\0bar"); } } ?> 3. private members should be named using "\0" . class name . "\0" . member name, like so: <?php class Foo { private $bar; public function __sleep() { return array("\0Foo\0bar"); } } ?> So with this information let us serialize a class hierarchy correctly: <?php class Base { private $foo = "foo_value"; protected $bar = "bar_value"; public function __sleep() { return array("\0Base\0foo", "\0*\0bar"); } } class Derived extends Base { public $baz = "baz_value"; private $boo = "boo_value"; public function __sleep() { // we have to merge our members with our parent's return array_merge(array("baz", "\0Derived\0boo"), parent::__sleep()); } } class Leaf extends Derived { private $qux = "qux_value"; protected $zaz = "zaz_value"; public $blah = "blah_value"; public function __sleep() { // again, merge our members with our parent's return array_merge(array("\0Leaf\0qux", "\0*\0zaz", "blah"), parent::__sleep()); } } // test it $test = new Leaf(); $s = serialize($test); $test2 = unserialize($s); echo $s; print_r($test); print_r($test2); ?> Now if you comment out all of the __sleep() functions and output the serialized string, you will see that the output doesn't change. The most important part of course is that with the proper __sleep() functions, we can unserialize the string and get a properly set up object. I hope this solves the mystery for everybody. __sleep() does work, if you use it correctly :-) ---- Server IP: 69.147.83.197 Probable Submitter: 24.22.133.183 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php Edit -- https://master.php.net/note/edit/81492 Del: integrated -- https://master.php.net/note/delete/81492/integrated Del: useless -- https://master.php.net/note/delete/81492/useless Del: bad code -- https://master.php.net/note/delete/81492/bad+code Del: spam -- https://master.php.net/note/delete/81492/spam Del: non-english -- https://master.php.net/note/delete/81492/non-english Del: in docs -- https://master.php.net/note/delete/81492/in+docs Del: other reasons-- https://master.php.net/note/delete/81492 Reject -- https://master.php.net/note/reject/81492 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136625) next »