Database Class
Posted: Mon May 26, 2008 8:09 pm
I am trying to design a database class that supports multiple drivers. I did a quick googling, and most of the classes I have found seem pretty ugly. Each function contains a plethora of conditionals to determine which driver to use. Currently what I do, is have a separate class file for each driver (mysql.php, mssql.php, ect...). Each class contains the same functions definitions (with of course different syntax for each driver). The driver to be used is defined in a configuration file and I have a class (db.php) to handle which file to use. Below is something I just threw together to represent my design/structure:
mysql.php
db.php
Now in some other file, I would like to create a new database object, and use the methods inside the mysql.php and mssql.php class. However, if I want to do this, I need to add those functions to the db class itself:
Is there a way I can avoid having to redefine all the functions? Is there a better way to do this?
Thank you,
John
mysql.php
Code: Select all
<?php
class MySQL {
__construct() {}
public function query($input) {
//query code here
}
?>Code: Select all
class db {
private $driver;
__construct($driver) {
switch($driver) {
case "mysql":
require_once("mysql.php);
$driver = new MySQL();
break;
case "mssql":
require_once("mssql.php");
$driver = new MsSQL();
break;
}
}Code: Select all
$db = new db();
$db->query("SELECT * FROM ...");Code: Select all
class db {
private $driver;
__construct($driver) {
switch($driver) {
case "mysql":
require_once("mysql.php");
$driver = new MySQL();
break;
case "mssql":
require_once("mssql.php");
$driver = new MsSQL();
break;
}
public function query($input) {
$driver->query($input)
}
}Thank you,
John