Image

Listens: .hack//OUTBREAK - menu song

PHP5 makes php a much stronger player

With the release of PHP5 I have been converting a lot of the code in the o9soft classlibs to php5, the changes mostly are in the OOP area. PHP5 adds full support for constructors and destructors which makes for much more powerful classlibs.

One very good example of this is in our mySQL library, now having support for a destructor we can remove the need for a call to mysql::close() at the end of a script using the mysql lib. now upon the completion of the script the mysql::__destruct() method calls mysql::close() automatically.

Current mySQL destructor:


function __destruct()
{
    $this->debug("mySQL class destructor called",__FILE__,__LINE__,__CLASS__,__FUNCTION__);
    $this->close();
}



As you can see this is a very simple method, all it does is make a call to the debugger, and the close() method. In php4 having the automatic calling of a function was much more cumbersome.

Another very cool new feature in php5 is the ability to create abstract classes, if you do not know what that means... basically it means that a class defined as abstract can not have an object initiated directly, it must be extended. This feature has given us the ability to create a master abstract class that all of our classes are build from that gives all of the classes references to $_err, $_debug, $_db, and $_var. This removes the need for any global statements in any class methods, or having to setup the references in each class definition. Ok to be completely honest this feature wasn't really necessary to implement this master class idea, i just haven't thought of it till now. Previously i could have setup a master class that was not abstract and used that as the master. Having the abstract class removes any problems that might occur if somehow an instance of the master class was created.

Here is a scaled down version of our master class:


abstract class std
{
   // These variables hold the references to $_err,$_debug,$_db and $_var
   private $err;
   private $debug;
   private $db;
   private $vars;
	
   // Constructor
   function __construct()
   {
      // Setup References to global objects
      $this->db     = &$GLOBALS['_db'];
      $this->err    = &$GLOBALS['_err'];
      $this->debug  = &$GLOBALS['_debug'];
      $this->vars   = &$GLOBALS['_var'];
   }

   // This method is a wrapper for the error loging function
   public function error($type = 0, $file = 0, $line = 0, $class = 0, $func = 0)
   {
      ...
   }
	
   // This method is a wrapper for the debug function
   public function debug($msg = 0, $file = 0, $line = 0, $class = 0, $func = 0)
   {	
      ...
   }
};



Not a whole lot to that. If an object were to be created from that class php would spit out an error like the one below.

Fatal error: Cannot instantiate abstract class std in /www/o9soft/dev/lib/std.php on line 4

And thats all for today