Click to See Complete Forum and Search --> : [PHP5] database abstraction


whisher06
06-06-2007, 05:56 PM
What do you think about this approach ?
It's the first draft.

<?php
class InvalidArgException extends Exception{
public function __construct($message, $code = 0){
parent::__construct($message, $code);
}
public function __toString(){
return __CLASS__ . " Line : [{$this->line}] Code : [{$this->code}] Msg : {$this->message}\n";
}
}
class DbException extends Exception{
public function __construct($message, $code = 0){
parent::__construct($message, $code);
}
public function __toString(){
return __CLASS__ . " Line : [{$this->line}] Code : [{$this->code}] Msg : {$this->message}\n";
}
}
class DBFactory{
private static $instance = null;
public static function createDB($db,$options){
if($db!='MySQL'&&$db!='SQLite'){
throw new Exception('Invalid type of database class');
}
return self::getInstance($db,$options);
}
private static function getInstance($db,$options){
if(is_null(self::$instance)){
self::$instance = new $db($options);
}
return self::$instance;
}
}
abstract class DBConnector{
abstract private function connect();
abstract public function query($query);
abstract public function close();
}
class MySQL extends DBConnector{
private $host='';
private $user='';
private $password='';
private $database='';
public $mysqli=null;
public function __construct($options=array()){
if(count($options)<4){
throw new InvalidArgException('Invalid options number in class ['.__CLASS__.']');
}
foreach($options as $parameter => $value){
if(empty($value)){
throw new InvalidArgException('Invalid parameter.Parameter ['.$parameter.'] is empty in class ['.__CLASS__.']');
}
$this->{$parameter}=$value;
}
$this->connect();
}
private function connect(){
$this->mysqli = @new mysqli($this->host,$this->user,$this->password,$this->database);
if(mysqli_connect_errno()){
throw new DbException('Connect failed: '.mysqli_connect_error());
}
}
public function query($query){
$mysql_query= new MySQL_query($this);
return $mysql_query->query($query);
}
public function close(){
$this->mysqli->close();
$this->__destruct();
}
final public function __destruct(){
unset($this->host);
unset($this->user);
unset($this->password);
unset($this->database);
}
}//
class MySQL_query{
private $mysqli=null;
public function __construct(MySQL $msql){
$this->mysqli=$msql;
}
public function query($query){
if(!$this->result=$this->mysqli->mysqli->query($query)){
throw new DbException('Error query : ['.$query.']');
}
if($this->result instanceof mysqli_result){
return new Result($this->result);
}
}
}
class Result{
private $result;
public function __construct(mysqli_result $mysqliResult){
$this->result = $mysqliResult;
}
public function fetchObject(){
return $this->result->fetch_object();
}
}
$options=array('host'=>'localhost','user'=>'','password'=>'','database'=>'');
$db= DBFactory::createDB('MySQL',$options);
$result= $db->query('SELECT * FROM users');
while($obj=$result->fetchObject()){
echo $obj->username;
}
$db->close();
?>


Any comments on how I should be doing this better would be gratefully appreciated. :D

MarkR
06-07-2007, 07:36 PM
Umm, you're reinventing the wheel. Not only has this been done before, it's been done before several times.

One notable implementation is PDO, which has fairly good MySQL and sqlite interfaces already (The quality of other drivers is variable). Not only that, it ships with PHP 5.1

Mark

whisher06
06-08-2007, 11:15 AM
Umm, you're reinventing the wheel. Not only has this been done before, it's been done before several times.
Yeah, I know it was just to practice and to master the OOP ;)

One notable implementation is PDO, which has fairly good MySQL and sqlite interfaces already (The quality of other drivers is variable). Not only that, it ships with PHP 5.1

Next step.


Best wishes.