Page 1 of 1

Database Class

Posted: Mon May 26, 2008 8:09 pm
by SidewinderX
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

Code: Select all

<?php
 
class MySQL {
 
__construct() {}
 
public function query($input) {
//query code here
}
?>
db.php

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;
         }
}
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.

Code: Select all

$db = new db();
$db->query("SELECT * FROM ...");
However, if I want to do this, I need to add those functions to the db class itself:

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)
    }
}
Is there a way I can avoid having to redefine all the functions? Is there a better way to do this?

Thank you,
John

Re: Database Class

Posted: Mon May 26, 2008 8:28 pm
by Christopher
Factory:

Code: Select all

 
class db {
    function getInstance($type) {
        switch($type) {
             case "mysql":
                 require_once("mysql.php);
                 $driver = new MySQL();
                 break;
             case "mssql":
                 require_once("mssql.php");
                 $driver = new MsSQL();
                 break;
         }
         return $driver;
     }
}
 
$db = db::getInstance('mysql');
You are essentially rebuilding PEAR DB and many others. What does your interface look like?

Re: Database Class

Posted: Tue May 27, 2008 12:30 am
by Mordred
SidewinderX wrote:Is there a way I can avoid having to redefine all the functions?
__call

Re: Database Class

Posted: Tue May 27, 2008 7:35 am
by allspiritseve
I used to have a class like this... then I switched to PDO. I believe it supports at least 5 or 6 drivers, MySQL included (that's all I use). Once you start binding values, you can never go back :D

Re: Database Class

Posted: Wed May 28, 2008 4:30 am
by Benjamin
allspiritseve wrote:I used to have a class like this... then I switched to PDO. I believe it supports at least 5 or 6 drivers, MySQL included (that's all I use). Once you start binding values, you can never go back :D
Care to share some examples? I believe my framework may already do this in a limited capacity.

Re: Database Class

Posted: Wed May 28, 2008 8:07 am
by allspiritseve