Jump to content

My second attempt with classes - Any Advice?


TeddyKiller

Recommended Posts

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

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

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 :P we use switch here ;)

Link to comment
Share on other sites

I noticed that switch/select too.

Also..

'mysql': should of been case 'mysql': :D

 

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.