Database Class

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Database Class

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Database Class

Post 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?
(#10850)
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Database Class

Post by Mordred »

SidewinderX wrote:Is there a way I can avoid having to redefine all the functions?
__call
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Database Class

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Database Class

Post 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.
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: Database Class

Post by allspiritseve »

Post Reply