The Gippy Pages


MySQL Class Updated for PHP 5 part 1

Mar 6, 2006 | PHP Scripting | By Shawn Parker

I’ve updated the SQL class to use some native PHP 5, namely constructors and destructors and include some backward compatibility so PHP 4 does not freak out when it loads the class.

I’ll hopefully be making this 100% PHP 5 native soon enough as I’ll actually have a native PHP 5 environment to apply it to.

Namely the class makes use of register_shutdown_function and call_user_func_array to simulate constructor behavior on PHP 4. So by doing this:

function dbConnect($settings=array()) {
  if(phpversion('tidy') < 5) {
    // construct
    $argcv = func_get_args();
    call_user_func_array(array(&$this, '__construct'), $argcv);
    // destruct
    register_shutdown_function(array(&$this), '__destruct');

I can now use __construct and __destruct in a PHP 4 compatible script and have the script be a little more PHP fun. The main advantage is that this script can close the db connection by itself now instead of having to do it in the page.

Hopefully this is useful to somebody…

You can view the entire source here.

Tagged as: class, code, database, dbconnect, mysql, oop, php, php4, php5, script, sql