Published by nick on 23 Sep 2007 at 10:07 am
When NOT to use a database abstraction layer in PHP
The most common reason that people cite for using a database abstraction layer is — "You can change the underlying database and your application doesn’t need to be rewritten."
From an academic perspective, this makes sense, and it’s hard to argue with. Especially in an interview or when you are talking to the fresh college grad with a Computer Science degree.
In the real world, it doesn’t happen.
Show me one person that has built their application with one database in mind, and then switched to another without having to rewrite some code, and I’ll show you 200 others that built their project using a database abstraction layer and they will never change the underlying database.
The concept of database neutrality is valuable under certain circumstances. If you are writing an open source software platform such as MediaWiki, and people that you don’t know will be installing the software on their own servers, and you want them to be able to use the database of their choice, then using a database abstraction layer for database neutrality is a clear winner.
There are other good reasons to use database abstraction layers:
- Convenience methods that you want to be able to reuse. My personal favorite example is getRowFromSql(), where you give it a sql statement that returns 1 row, and it will return an associative array with the columns/values.
- Common configuration settings. Ie, your username, password, and host information are all in one place.
- Central configuration of write vs. read connections. Do this right from the start, because you’ll need it when you go to scale mysql.
- Reuse of database connections for performance
These are enough for me, and they are the reason I use database abstraction layers. But if the only reason you are using a database abstraction layer is so that you can easily switch your database to something else in the future, you’re nuts, and you may want to make sure you are following the number one rule of software.
Me personally, I just have a simple class that extends PDO.