XenoPhage Posted March 15, 2006 Share Posted March 15, 2006 Ok, this one's not for the light hearted.. :) This is PHP 4.4.0 (gentoo build)Here's some code to start us out (I've cut a LOT out of this) :[code]class Outage { var outage_id; var outage_dirty; function Outage(&$dbconn, $oid = -1) { if (($oid != -1) && is_numeric($oid)) { // Load some data from the database $this->outage_id = $oid; $this->outage_dirty = false; } else { // New outage $this->outage_id = -1; $this->outage_dirty = true; } // Register a shutdown function register_shutdown_function( array(&$this, '_check_dirty_flags') ); // Outage creation successful - return true return true; } // Set and/or return the Outage ID function outage_id($outage_id = -1) { if (($outage_id != -1) && is_numeric($outage_id)) { $this->outage_id = $outage_id; } // If the current outage ID is -1, this may be a new outage and we need // to serialize it. if ($this->outage_id == -1) $this->_check_dirty_flags(); return $this->outage_id; } // Serializes (saves) any changes to the outage function _serialize_outage() { // -1 signifies a new outage if ($this->outage_id == -1) { // Save this new outage to the database $this->outage_id = mysql_insert_id(); } else { // Update this existing outage in the database } $this->outage_dirty = false; } // Shutdown function // Checks the flags to see if any serialization is needed function _check_dirty_flags() { if ($this->outage_dirty) { $this->_serialize_outage(); } }}[/code]Ok, some explanation. I create an instance of the outage class at the beginning of my program. In the direct instance I'm talking about, this is a new outage. The code has a flow something like this (note, in this example, assume that $_REQUEST['outage_id'] is not set :[code] // Load the defaults require_once('outage_glob.php'); // Connect to the MySQL database $sqlhdlr = mysql_connect($DB_HOST, $DB_LOGIN, $DB_PASS) or die('Could not connect: ' . mysql_error()); mysql_select_db($DB_NAME) or die('Could not select database'); // Create the outage object (all sanitization is handled by the object) if (isset($_REQUEST['outage_id'])) { $outage_obj = new Outage($sqlhdlr, $_REQUEST['outage_id']); } else { $outage_obj = new Outage($sqlhdlr); } // Set a bunch of stuff up in the outage $outage_obj->mymethod('stuff'); // Now we display the outage id print $outage_obj->outage_id(); // and exit exit;[/code]The behaviour sought here is for the outage to have been serialized when $outage_obj->outage_id() was called, and when the code exits, the shutdown function will see the outage_flag as false, thus not bothering to serialize the outage again. However, for some reason, it sees the flag as dirty when the shutdown function is called, and it serializes again. This wouldn't be so bad, but it also sees the outage_id as -1, so it thinks it's a new outage.Can PHP handle this? Am I doing something wrong here? I can always change my program slightly to call the serialization function just before exiting, but I was hoping to add some additional functionality and check for user aborts as well. I wanted to get this working first, though..HELP!Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.