<?php
// sample code to illustrate the use of factory design pattern
// Each class and the actual implementation logic can be in separate files.
// http://royads.blogspot.com
class mysqlClass
{
function __construct($config = array())
{
echo 'connecting to mysql server ..';
}
}
class mssqlClass
{
function __construct($config = array())
{
echo 'connecting to mssql server ..';
}
}
class Database
{
// This is a factory method because it allows us to instantiate other class
public static function dbFactory($db,$config = array())
{
switch($db)
{
case "mysql" :
// load the required class if its in another php file
// create the object of selected database class
$dbObject = new mysqlClass();
break;
case "mssql" :
// load the required class if its in another php file
// create the object of selected database class
$dbObject = new mssqlClass();
break;
}
return $dbObject;
}
}
$db = Database::dbFactory('mysql',array('host','username','pass','dbname'));
echo "\n";
$db = Database::dbFactory('mssql',array('host','username','pass','dbname'));
?>