“PHP Database classes” make my eyes bleed.

May 22, 2008 – 11:31 pm

Through the maze of duplicate entries I encountered yesterday through StumbleUpon, I came across this page, which reads:

A class for very basic MySQL database connectivity. Written to reduce redundant code in my own projects aswell as aid in debugging and error reporting during the developement phase.

Ok, fair ’nuff, probably nothing but, suspecting atrocity, I downloaded and took a look at the actual code. In this post, I’ll discuss the page in question, as well as rant about so-called PHP database classes in general, which, in general, suck, to put it bluntly.

Facepalm.

As I suspected, this so-called ‘class written to reduce redundant code’ or, as the comment in the code itself says, ‘ It simplifies the database tasks I most often need to do thus reducing redundant code.’, is nothing more but a set of basic wrappers around PHP’s built-in MySQL functions. I have no clue why this stuff is actually in a class, and I really can’t grasp why there’s a shiteload of comments at the top, listing the author’s e-mail, website, copyright thingy, version number and history (!) and everything.

Some excerpts from the code itself, for the lulz:


// blah
$this->host = 'localhost';
$this->user = '';
$this->pw = '';
$this->db = '';

$this->auto_slashes = true;
}

function connect($host=”, $user=”, $pw=”, $db=”, $persistant=true) {

// Opens a connection to MySQL and selects the database. If any of the
// function’s parameter’s are set, we want to update the class variables.
// If they are NOT set, then we’re giong to use the currently existing
// class variables.
// Returns true if successful, false if there is failure.

if (!empty($host)) $this->host = $host;
if (!empty($user)) $this->user = $user;
if (!empty($pw)) $this->pw = $pw;
// rest ommitted for nao

Okay, so you have an empty class user, password and database. If the user is empty, leave the username empty. If it’s not, fill it in. In other words,


if ($user == '') $user = '';

wtf?

There’s probably a shitload more of that kinda retarded thinking in there, I’m not going to bother with that. Instead, I’ll move this on to a more generic rant about these so-called PHP database classes.

It has come to my attention that a large amount of these so-called PHP database classes, which are supposedly all an attempt at object-oriented programming (hence the ‘classes’), are all basically just wrappers around PHP’s built-in MySQL functions, with some error checking and, zomg, class variables to store data such as connection data in. The problem however is that most of these classes are called “db”, “Database”, or “mysql_database” or whatever. When I see a class named “database”, I instantly feel a rising blood pressure behind my eyes. Why? Because naming a class Database means that instances of these classes are databases themselves - even though they’re not, but a means to access a database. Naming is serious business, and I’m not even using that term in the sarcastic sense - a class’ name tells people what it is, not what it’s used for, nor should it be the only thing you could come up with. (If it’s the latter, name the class “PleaseThinkOfANameForThisClassSeriouslyElseYouHaveToTypeOrCopypastaThisEntireClass
NameEveryTimeYouWantToMakeAnInstanceOfItForGreatJustice”), plox.

So next time you name a class Database or something like it, please make it persistently store massive amounts of tabular, relational data in it. For great justice.




A class named Database not only implies there’s a lack of understanding what the author is actually attempting to create, but it also implies that the author has no fucking clue what object-oriented programming is, and has probably seen someone else put his functions in between curly braces with the word ‘class’ at the top so he could use the same function name twice in his program.

Most of these ‘database classes’ have a constructor that allows the user to specify a database host, username, password, and a database name. Okay, makes sense, but only if the constructor is for a database connection class, that represents a connection to a certain database.

What usually comes next however doesn’t make much sense (that is, if you know what object-oriented programming is). They add a function called ‘query($sql)’, with witty comments like ‘Executes a query on the database’ (insert your own extra additional comments at will, preferably using PHPDoc-style tags indicating that the $sql parameter should contain the query and the result is a MySQL result). But in object-oriented programming, that wouldn’t make sense in a database connection class - a database connection doesn’t execute queries.

The list goes on, with functions like num_rows($result), which returns the amount of rows returned by the result of the aforementioned query function, get_array($result) which returns an array with a row from the result, etcetera. Cute, but there’s built-in PHP functions for that, so quit abusing classes to do that. Okay, so there’s error checking or something in there - if there is an error at this point, it’s because you did something wrong, not because PHP’s mysql_fetch_array() function might cause an error that you might have to catch in your would-be database-class-that-is-not-a-class-but-a-list-of-functions.

In a properly object-oriented implementation of most of these database classes, you’d first create a DatabaseConnection, which establishes a connection with the database. Next you’d call a selectDatabase($database) function on it, which is just a wrapper for mysql_select_db (yay!), and then you’d pass your connection to a QueryExecuter, along with a Query object that smartly assembles queries based on the parameters you’ve passed to it - table to get rows from, columns to get, parameters, joins, etc, all with rich amounts of object-oriented sauce. I’m not planning on chewing out a PHP database connectivity package that does the above, since there’s hordes of people way smarter than me that have already done such a thing over and over and over and over and over again.

‘Cause that’s PHP’s main tagline, it seems - if the wheel already exists, re-invent it. This is also obvious from the thousands of attempts at database connectivity packages, homebrew template engines, etcetera.

I have to make a confession though: Back when I was still working with PHP, I was guilty of the same thing. I used a database class created by some other dude (who was at the time actually making something like $35-$45 an hour for creating PHP webapps), which consisted of all the things I listed above, and in a later project, I made my own, similar database class. I should look it up, for the lulz.




Looked up, zomg it’s even worse than I expected. There’s more lines of error messages than of actual code.

Anyways, that was years and years ago (read: ~2 years), so let’s forget about that one.

But here are the basic conclusions of this rant:

  1. Don’t write your own database class(es) if you don’t really know what you’re doing.
  2. Don’t re-invent the wheel. again. Even if it’s PHP and it’s considered a Good Thing to do everything yourself.
  3. Use other people’s work. They’re usually smarter in this area then you’ll be, at least for the next five or ten years, and then you too will have realized that PHP isn’t all that good. Or, you should have realized by then.

In the follow-up, we’ll expand on this topic and see what a search on Google for ‘PHP Database class‘ comes up with. Then we’ll lulz.

Obligatory list of social network links:

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • bodytext
  • del.icio.us
  • Google
  • E-mail this story to a friend!
  • Print this article!
  • Reddit
  • Slashdot
  • StumbleUpon
  1. 2 Responses to ““PHP Database classes” make my eyes bleed.”

  2. oh nevermind. here IS a good example. thanks

    By JD on Nov 12, 2008

  1. 1 Trackback(s)

  2. May 24, 2008: “PHP Database Classes” - Follow-Up | For Great Justice

Post a Comment