Jump to content


This topic is now archived and is closed to further replies.


Advanced OOP Class Question

Recommended Posts

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) :

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)

      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) {

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 :

   // Load the defaults

   // 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

   // Now we display the outage id
   print $outage_obj->outage_id();

   // and exit

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..



Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.