With the maturing of PHP, and the final inclusion of deconstructors in the up and coming PHP 5, I’ve opted to finally get rid of some of my old cruft, and start making it OOP.
This code is not the world’s best example, but one of the first things I’ve done is rewritten the SQL database shim; this tricky little bugger allows me to account for the differences in SQL layers within PHP. Currently, it supports MySQL and PostgreSQL (it supported DBX, but I’m going to rewrite it for SQLite shortly).
What’s the difference? Quite a bit. The abstraction, with a bit of logic, allows for several simutaneous connections without fear of ‘treading on toes’ as dbshim was initally written to manage the database connection IDs by itself, however, this logic was not introduced until the move to OOP.
Find below a rather icky (elder) version from my intial revisions of dbshim:
function my_db_connect() { global $mysqlindex, $mydbhost, $mydbusername, $mydbpassword, $mydbname, $mydbtype; if ($mydbtype "mysql") { $mysqlindex = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to connect to server."); mysql_select_db($dbname) or die("Unable to select database."); } elseif ($mydbtype “pgsql”) { $mysqlindex = pg_connect(“host=$dbhost dbname=$dbname user=$dbuser password=$dbpass”); } elseif ($dbtype == “dbx”) { // eventually, we’ll have to do SOMETHING, I think } }This would be called by:
my_db_connect();It would happily usurp global variables, which were only available to dbshim, but were quite bad, regardless.
Rewritten in OOP, it looks like this: class rollatorDB { function rollatorDB($dbhost, $dbuser, $dbpass, $dbname, $dbtype) { $this->mydbtype = $dbtype; switch ($this->mydbtype) { case “mysql”: $this->mysqlindex = mysql_connect($dbhost, $dbuser, $dbpass) or $this->error(“Unable to connect to server.”); mysql_select_db($dbname, $this->mysqlindex) or $this->error(“Unable to select database.”); break; case “pgsql”: $this->mysqlindex = pg_connect(“host=$dbhost dbname=$dbname user=$dbuser password=$dbpass”); break; default: $this->error(“Unable to connect: Unsupported DBType given!”); } } /* Other functions follow… */ }
As you can see, I switched to, er, switch statements, and have added an internal error system to this shim, so it won’t explictly call functions outside of itself, which is considered to be bad.
The new functionale is called as such:
require ‘rollatorDB.php’;$myDB = new rollatorDB(”$mydbhost”, ”$mydbuser”, ”$mydbpass”, ”$mydbname”, ”$mydbtype”);
It’s different. I’ll give it that.
Where I used to use $result = my_db_query(“SELECT foo FROM bar WHERE quux=’baz’”); , I’ll now be using $result = $myDB->my_db_query(“SELECT foo FROM bar WHERE quux=’baz’”); Update: Now, well, I’ll actually be using $result = $myDB->dbQuery(“SELECT foo FROM bar WHERE quux=’baz’”);
It’s nice, since I can have several instances, if need be, with no global variable pollution.
The code is getting tighter, despite the silliness above; and by using switch statements, it’s not only more legible, but fairly trivial to add support for future databases.
Update: Since taking the plunge, I’ve rewritten the whole thing, as you can see for yourself, and even converted the elder style underscores to studlyCaps. ;)