View Single Post
Old 05-30-2008, 09:14 PM   #2 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

The approach I would take would be something like the following. First I would create an interface to hold all the child class methods that I'll be needing for my database class:

php Code:
interface Database_Interface
{
    public function insertRow($aData);
    public function updateRow($aData, $iRowId);
    public function deleteRow($iRowId);
}

Then I would create the child classes, one for each single database type possible, which all implement that interface:

php Code:
class Database_PostgreSQL implements Database_Interface
{
    public function insertRow($aData) { return 'Insert PostgreSQL'; }
    public function updateRow($aData, $iRowId) { return 'Update PostgreSQL'; }
    public function deleteRow($iRowId) { return 'Delete PostgreSQL'; }
}

class Database_MySQL implements Database_Interface
{
    public function insertRow($aData) { return 'Insert MySQL'; }
    public function updateRow($aData, $iRowId) { return 'Update MySQL'; }
    public function deleteRow($iRowId) { return 'Delete MySQL'; }
}

class Database_MySQLi implements Database_Interface
{
    public function insertRow($aData) { return 'Insert MySQLi'; }
    public function updateRow($aData, $iRowId) { return 'Update MySQLi'; }
    public function deleteRow($iRowId) { return 'Delete MySQLi'; }
}

And finally add the database base which would be the class that's first initialised:

php Code:
class Database
{
    private $m_pDatabase;
   
    public function setDatabase(Database_Interface $pDatabase)
    {
        $this->m_pDatabase = $pDatabase;
    }
   
    public function insertRow($aData)
    {
        return $this->m_pDatabase->insertRow($aData);
    }
   
    public function updateRow($aData, $iRowId)
    {
        return $this->m_pDatabase->updateRow($aData, $iRowId);
    }
   
    public function deleteRow($iRowId)
    {
        return $this->m_pDatabase->deleteRow($iRowId);
    }
}

Then last of all we would initialise that class we've just created, and set the database we want to use by using the setDatabase function. By doing that, we can seamlessly change database types, as the majority of the code is kept in the child classes.

php Code:
$pDb = new Database();
$pDb->setDatabase(new Database_PostgreSQL());
echo $pDb->insertRow(array(1, 2));

As we have set the database type to PostgreSQL, any function calls will be routed to the PostgreSQL child class. Voila!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.

Last edited by Wildhoney : 05-31-2008 at 02:10 AM.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following 3 Users Say Thank You to Wildhoney For This Useful Post:
coiyeun2b (06-29-2008), delayedinsanity (05-30-2008), maZtah (06-02-2008)