TeddyKiller Posted May 12, 2010 Author Share Posted May 12, 2010 I'm use to mysql_* so maybe the abstract classes too is just confusing me more. So for now, we can leave that function empty.. for sake of less confusion. So.. cutting down most of the functions. What I should have.. is this. <?php class DatabaseAdapterFactory { public static function factory($adapterName) { //@returns any child of DatabaseAdapter } } interface DatabaseAdapterInterface { public function __construct($server, $username, $password, $database); public function connect(); public function connect_db(); public function select_db(); public function execute($sql); } abstract class DatabaseAdapterAbstract implements DatabaseAdapterInterface { } class MySQLAdapter extends DatabaseAdapterAbstract { private $server = ""; private $username = ""; private $password = ""; private $database = ""; private $connection = ""; /* Construct variables .. thing */ public function __construct($server, $username, $password, $database) { $this->server = $server; $this->username = $username; $this->password = $password; $this->database = $database; } /* Connect to database and select database */ public function connect() { $this->connection = $this->connect_db($this->server, $this->usernname, $this->password); if (!$this->connection) print '<strong>Error:</strong> Connection to Database Failed'; if (!$this->select_db($this->database, $this->connection)) print '<strong>Error:</strong> Cannot select Database'; $this->query("SET NAMES 'utf8'", $this->connection); $this->query("SET CHARACTER SET 'utf8'", $this->connection); unset($this->password); } /* Connect to the database */ public function connect_db() { return mysql_connect($this->server, $this->username, $this->password); } /* Select the database */ public function select_db() { return mysql_select_db($this->database, $this->connection); } /* Perform mysql_query */ public function execute($sql) { if (trim($sql) != "") : $this->query_id = mysql_query($sql, $this->connection); endif; if (!$this->query_id) : print "MySQL Error on Query : " . $sql; endif; return $this->query_id; } } ?> If thats right.. public static function factory($adapterName) { } how would I code in that function to choose the right adapter? Then the ultimate question after all that, how do I use it.. Quote Link to comment Share on other sites More sharing options...
trq Posted May 12, 2010 Share Posted May 12, 2010 If thats right.. public static function factory($adapterName) { } how would I code in that function to choose the right adapter? It could be as simple as.... class DatabaseAdapterFactory { public static function factory($type, $user, $pass, $server, $db) { select ($type) { 'mysql': $db = new MySQLAdapter($server, $user, $pass, $db); break; // etc etc for each type } return $db; } } $db = DatabaseAdapterFactory::factory('mysql', 'foo', 'bar', 'localhost', 'example'); Quote Link to comment Share on other sites More sharing options...
ignace Posted May 12, 2010 Share Posted May 12, 2010 If thats right.. public static function factory($adapterName) { } how would I code in that function to choose the right adapter? It could be as simple as.... class DatabaseAdapterFactory { public static function factory($type, $user, $pass, $server, $db) { select ($type) { 'mysql': $db = new MySQLAdapter($server, $user, $pass, $db); break; // etc etc for each type } return $db; } } $db = DatabaseAdapterFactory::factory('mysql', 'foo', 'bar', 'localhost', 'example'); select?? seriously, stay away from VB we use switch here Quote Link to comment Share on other sites More sharing options...
trq Posted May 12, 2010 Share Posted May 12, 2010 ha!! I don't even notice I do it. Been working with asp all day (and dreading it). Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted May 12, 2010 Author Share Posted May 12, 2010 I noticed that switch/select too. Also.. 'mysql': should of been case 'mysql': so with that.. I can still use my method of.. $db->execute for the queries? Quote Link to comment Share on other sites More sharing options...
ignace Posted May 12, 2010 Share Posted May 12, 2010 I noticed that switch/select too. Also.. 'mysql': should of been case 'mysql': so with that.. I can still use my method of.. $db->execute for the queries? Yup it will return the appropriate object and as they all come from the same mother they all share the same functionality. $db = DatabaseAbstractFactory::factory('mysql://ignace:password@localhost/database'); $db->execute('SELECT * FROM table'); echo get_class($db);//MySQLAdapter $db2 = DatabaseAbstractFactory::factory('mssql://ignace:password@localhost2/database'); $db->execute('SELECT * FROM table'); echo get_class($db2);//MsSQLAdapter Neat ha Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted May 12, 2010 Author Share Posted May 12, 2010 $db2 = DatabaseAbstractFactory::factory('mssql://ignace:password@localhost2/database'); $db->execute('SELECT * FROM table'); Should be $db2 Though what is the echo get_class($var) ? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted May 12, 2010 Share Posted May 12, 2010 It echo's the class name of the instantiated object. This way you can be sure your factory is returning instances of the correct class. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.