note 98963 added to language.oop5.magic

From: Date: Sun, 18 Jul 2010 22:20:41 +0000
Subject: note 98963 added to language.oop5.magic
Groups: php.notes 
Request: Send a blank email to php-notes+get-170960@lists.php.net to get a copy of this message
Note a common pitfall when using __wakeup. If you unserialize a datastructure, you may not rely on the parent object to have been fully unserialized by the time __wakeup is called. Example class A { public $b; public $name; } class B extends A { public $parent; public function __wakeup() { var_dump($parent->name); } } $a = new A(); $a->name = "foo"; $a->b = new B(); $a->b->parent = $a; $s = serialize($a); $a = unserialize($s); Expected output: "foo". Actual output: NULL. Reason: $b is unserialized before $name. By the time B::__wakeup is called, $a->name does not yet have a value. So be aware that the order in which your class variables are defined is important! You need to manually order them by dependencies - or write a __sleep function and order them by depencies there. (Currently I can't tell which option I hate more) ---- Server IP: 212.124.37.9 Probable Submitter: 77.0.125.121 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php Edit -- https://master.php.net/note/edit/98963 Del: integrated -- https://master.php.net/note/delete/98963/integrated Del: useless -- https://master.php.net/note/delete/98963/useless Del: bad code -- https://master.php.net/note/delete/98963/bad+code Del: spam -- https://master.php.net/note/delete/98963/spam Del: non-english -- https://master.php.net/note/delete/98963/non-english Del: in docs -- https://master.php.net/note/delete/98963/in+docs Del: other reasons-- https://master.php.net/note/delete/98963 Reject -- https://master.php.net/note/reject/98963 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#170960) next »