note 40908 added to function.register-shutdown-function

From: Date: Thu, 18 Mar 2004 16:57:34 +0000
Subject: note 40908 added to function.register-shutdown-function
Groups: php.notes 
Request: Send a blank email to php-notes+get-67028@lists.php.net to get a copy of this message
When using the register_shutdown_function command in php 4. The registered functions are called in the order that you register them. This is important to note if you are doing database work using classes that register shutdown functions for themselves. You must register the shutdown_functions in the order that you want things to shutdown. ( ie the database needs to shutdown last ) Example of what will not work but what you might expect to work :- <? class database { function database() { echo "connect to sql server -- (database :: constructor)<br>\n"; register_shutdown_function( array( &$this, "phpshutdown" ) ); $this->connected = 1; } function do_sql( $sql ) { if ( $this->connected == 1 ) { echo "performing sql -- (database :: do_sql)<br>\n"; } else { echo " ERROR -- CAN NOT PERFORM SQL -- NOT CONNECTED TO SERVER -- (database :: do_sql)<br>\n"; } } function phpshutdown() { echo "close connection to sql server -- <b>(database :: shutdown)</b><br>\n"; $this->connected = 0; } } class table { function table( &$database, $name ) { $this->database =& $database; $this->name = $name; echo "read table data using database class -- name=$this->name -- (table :: constructor)<br>\n"; register_shutdown_function( array( &$this, "phpshutdown" ) ); $this->database->do_sql( "read table data" ); } function phpshutdown() { echo "save changes to database -- name=$this->name -- <b>(table :: shutdown)</b><br>\n"; $this->database->do_sql( "save table data" ); } } $db =& new database(); $shoppingcard =& new table( &$db, "cart " ); ?> Output of the above example is :- connect to sql server -- (database :: constructor) read table data using database class -- name=cart -- (table :: constructor) performing sql -- (database :: do_sql) close connection to sql server -- (database :: shutdown) save changes to database -- name=cart -- (table :: shutdown) ERROR -- CAN NOT PERFORM SQL -- NOT CONNECTED TO SERVER -- (database :: do_sql) ---- Manual Page -- http://www.php.net/manual/en/function.register-shutdown-function.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+40908 Delete -- http://master.php.net/manage/user-notes.php?action=delete+40908&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+40908&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#67028) next »