Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    [PHP5] database abstraction ..........

    Ciao che ne pensate di un approccio del genere
    Codice PHP:
    <?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 
    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 
    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();
        }
    }
    try {
    $options=array('host'=>'localhost','user'=>'','password'=>'','database'=>'');
    $dbDBFactory::createDB('MySQL',$options);
    $query= new MySQL_query($db);
    $result$query->query('SELECT * FROM users');
    while(
    $obj=$result->fetchObject()){
     echo 
    $obj->username;
    }
    $db->close();
    }
    catch (
    InvalidArgException $e) {
        echo 
    $e;
        
    error_log($e->getMessage()."\n"3$root."/log/error.log");
        exit();
    }
    catch (
    DbException $e) {
        echo 
    $e;
        
    error_log($e->getMessage()."\n"3$root."/log/error.log");
        exit();
    }
    ?>
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  2. #2
    Nella classe MySQL
    Codice PHP:
    public function query($query){    
            
    $mysql_query= new MySQL_query($this);
            return 
    $mysql_query->query($query);
        } 
    da usare:

    Codice PHP:
    $dbDBFactory::createDB('MySQL',$options);
    $result$db->query('SELECT * FROM users'); 
    Decisamente molto meglio


    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.