note 61130 added to language.oop5.magic

From: Date: Thu, 26 Jan 2006 11:18:44 +0000
Subject: note 61130 added to language.oop5.magic
Groups: php.notes 
Request: Send a blank email to php-notes+get-102477@lists.php.net to get a copy of this message
The sequence of events regarding __sleep and __destruct is unusual __ as __destruct is called before __sleep. The following code snippet: <?php $sequence = 0; class foo { public $stuff; public function __construct($param) { global $sequence; echo "Seq: ", $sequence++, " - constructor\n"; $this->stuff = $param; } public function __destruct() { global $sequence; echo "Seq: ", $sequence++, " - destructor\n"; } public function __sleep() { global $sequence; echo "Seq: ", $sequence++, " - __sleep\n"; return array("stuff"); } public function __wakeup() { global $sequence; echo "Seq: ", $sequence++, " - __wakeup\n"; } } session_start(); $_SESSION["obj"] = new foo("A foo"); ?> yields the output: Seq: 0 - constructor Seq: 1 - destructor Seq: 2 - __sleep Only when you end your script with a call to session_write_close() as in: <?php $sequence = 0; class foo { public $stuff; public function __construct($param) { global $sequence; echo "Seq: ", $sequence++, " - constructor\n"; $this->stuff = $param; } public function __destruct() { global $sequence; echo "Seq: ", $sequence++, " - destructor\n"; } public function __sleep() { global $sequence; echo "Seq: ", $sequence++, " - __sleep\n"; return array("stuff"); } public function __wakeup() { global $sequence; echo "Seq: ", $sequence++, " - __wakeup\n"; } } session_start(); $_SESSION["obj"] = new foo("A foo"); session_write_close(); ?> the sequence is as common sense would expect it to be as the following output shows: Seq: 0 - constructor Seq: 1 - __sleep Seq: 2 - destructor ---- Server IP: 64.246.30.37 Probable Submitter: 84.56.17.215 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.magic.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+61130 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+61130&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+61130&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+61130&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+61130&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+61130&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+61130&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+61130&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+61130&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102477) next »