Page 1 of 1

Im i doing this right (connection class, xyz class..)

Posted: Tue Jan 20, 2009 11:08 am
by originalseed
I have some basic connection and data access that i wrote long time ago, and im using them on every project. I was wondering if the logic is OK!

This is a conncetion class:

Code: Select all

 
class [color=#0000FF]DB_CONNECTION[/color]{
 
    private $dbhost;
    private $dbuser;
    private $dbpass;
    private $dbname;
    private $conn;
        
    function DB_CONNECTION(){
        $this->dbhost = 'localhost';
        $this->dbuser = 'root';
        $this->dbpass = '';
        $this->dbname = 'newdb';
    }
    
    protected function __createConnection(){
        return @mysql_connect($this->dbhost, $this->dbuser, $this->dbpass);
    }
    function __selectDB($connection){
        return @mysql_select_db($this->dbname, $connection);
    }
    public function __executeSQL($sql){
        $this->conn = $this->__createConnection();
        $this->__selectDB($this->conn);
        return @mysql_query($sql, $this->conn);
    }
    public function __closeConnection(){
        @mysql_close($this->conn);
    }
}
 
this is a data access class

Code: Select all

 
class [color=#008040]GET_DATA[/color] extends [color=#0000FF]DB_CONNECTION[/color]{
    
    public function [color=#008040]__pullData($sql)[/color]{
        $resoult = parent::__executeSQL($sql);
        $i=0;
        $numberfields = mysql_num_fields($resoult) or die(mysql_error()."<br>".$sql);
        while($red = mysql_fetch_array($resoult)){
            for ($j=0; $j<$numberfields; $j++ ) {
                $field_name = mysql_field_name($resoult, $j);
                $datapull[$i][$field_name] = $red[$field_name];
            }
        $i++;
        }
        return $datapull; // returns array
    }
        public function __insert($sql){....}
        public function __delete($sql){....}
}
 

using this classes:

Code: Select all

 
$config = Array('SQL_EXECUTER' => $getdataOBJ );
class [color=#BF0000]Page[/color] {
    // configuration array located in config.php 
    public static $config = array();
    private $getDataOBJ; 
    
    public function __construct(){
        
        $numargs = func_num_args();
        if ($numargs > 0 && is_array($config = func_get_arg(0))) {
            $this->config = $config;
        }
        
        $this->setConfiguration();
    }
    
    //seting configuration if not set in config.php
    protected function setConfiguration(){
        if(isset($this->config['SQL_EXECUTER'])){
            $this->getDataOBJ = $this->config['SQL_EXECUTER'];
        }else{
            Page_Error::raiseError(800);
        }
    }
    
    public function __listing(){
        $query = "SELECT * FROM gallery";
        return $this->getDataOBJ->__pullData($query);
    }
}//end class
 
in index page:

Code: Select all

 
        [color=#0000FF]$dbOBJ[/color] = new DB_CONNECTION();
    [color=#408040]$getdataOBJ[/color] = new GET_DATA();
 
// actions here.......
        [color=#BF0000]$page[/color] = new Page($config);
        $e = $page->__listing();
var_dump($e);
        
        $dbOBJ->__closeConnection();