关于数据库连接类的想法

时间:2013-08-09 15:04:29

标签: php mysql connection

我有一个简单的数据库类,基本上包含下面的函数,所以我决定让谨慎的类做同样的事情,我的问题具体是我需要在用户和密码类中为生产做好准备的功能环境

$db = new DB;
$link = $db->connect()->getLink();

class DB {

    public $connection = array();

    public function __construct() {
        return $this;
    }

    public function connect($host=null, $user=null, $pass=null, $database=null) {
        $this->connection = new Connection($host, $user, $pass, $database);
        $this->connection->connect();
        $this->link = $this->connection->getLink();
        if ($this->connection->ping()) {
            if (!is_null($database)) {
                if (!$this->connection->databaseConnect($database)) {
                    throw new\Exception('Unable to connect to the server.');
                }
            }
        }else{
            throw new \Exception('Unable to connect to the server.');
        }
        return $this;
    }

    public function getLink() {
        return $this->link;
    }

}

class Connection {

    protected $chost='localhost';
    protected $cuser='guest';
    protected $cpass='password';
    protected $cdatabase='PROFORDABLE';

    function __construct($host=null, $user=null, $pass=null, $database=null) {

        $host = !is_null($host) ? $host : $this->chost;
        $user = !is_null($user) ? $user : $this->cuser;
        $password = !is_null($pass) ? $pass : $this->cpass;
        $database = !is_null($database) ? $database : $this->cdatabase;

        $this->set('host', $host)->set('user', $user)->set('password', $password)->set('database', $database);
        return $this;
    }

    public function connect() {
        $link = mysqli_connect($this->host->getHost(), $this->user->getUser(), $this->password->getPassword());
        if (!is_object($link)) {
            throw new \Exception("An error has occurred while connecting to the server.");
        }
        $this->link = $link;
    }

    public function databaseConnect($database) {
        if (!mysqli_select_db($this->getLink(), $database)) {
            throw new \Exception("Unable to select the database.");
        }
    }

    public function getLink() {
        return $this->link;
    }

    public function ping() {
        if (mysqli_ping($this->link)) {
            return TRUE;
        }
        return FALSE;
    }

    public function set($name, $param) {
        if (!isset($name) || !isset($param)) return $this;
        $class = __NAMESPACE__.'\\'.ucwords($name);
        $this->$name = new $class($param);
        return $this;
    }

    public function get($name) {
        $getfunc = 'get'.ucwords($name);
        return $this->$name->$getFunc();
    }

}

class Host {
    public function __construct($host) {
        $this->setHost($host);
    }
    public function setHost($host) {
        $this->host = $host;
    }
    public function getHost() {
        return $this->host;
    }
}

class User {

    public function __construct($user) {
        $this->setUser($user);
    }

    public function setUser($user) {
        $this->user = $user;
    }

    public function getUser() {
        return $this->user;
    }

}

class Password {

    public function __construct($password) {
        $this->setPassword($password);
    }

    public function setPassword($password) {
        $this->password = $password;
    }

    public function getPassword() {
        return $this->password;
    }

    public function sha($value) {
        return sha1($value);

    }

}

class guestPassword extends Password {
    const PASSWORD='password';
    public function __construct() {
        return PASSWORD;
    }
}

class Database {

    public function __construct($database) {
        $this->setDatabase($database);
    }

    public function setDatabase($database) {
        $this->database = $database;
    }

    public function getDatabase() {
        return $this->database;
    }

}

class Query {


}

2 个答案:

答案 0 :(得分:0)

通常DB类也保持连接。制作单独的连接类是过度设计的。

我担心DB的API比内部更多。

答案 1 :(得分:0)

跟踪多个连接的任何帮助??

class DB {

public $connections = array();
public $threads = array();
public $lastThread=null;

public function __construct() {
    $this->connect();
    return $this;
}

public function connect($host=null,$user=null,$pass=null,$db=null) {
    $connection = new Connection($host, $user, $pass, $db);
    if (!$connection->ping() || !$connection->databaseConnect($connection->get('database'))) {
        throw new \Exception('ping or database select problem');
    }

    $threadId = mysqli_thread_id($connection->getLink());
    $this->connections[$threadId] = $connection;
    $this->threads[] = $threadId;
    $this->lastThread = $threadId;
    return $this;
}

public function getLink($threadId=null) {
    if (is_null($threadId)) {
        if (!isset($this->lastThread)
        return $this->connections[$this->lastThread]->getLink();

    }else{
        return $this->connections[$threadId]->getLink();
    }
    }

}

class Connection {

public $link;
public $host='localhost';
public $user='guest';
public $pass='password';
public $database='PROFORDABLE';

function __construct($host=null, $user=null, $pass=null, $database=null) {
    if (!is_null($host)) $this->host = $host;
    if (!is_null($user)) $this->user = $user;
    if (!is_null($pass)) $this->pass = $pass;
    if (!is_null($database)) $this->database = $database;
    $this->connect($this->host, $this->user, $this->pass);
    $this->databaseConnect($this->database);
    return $this;
}

public function connect() {
    $link = mysqli_connect($this->host, $this->user, $this->pass);
    if (!is_object($link)) {
        throw new \Exception("Not object");
    }
    $this->link = $link;
}

public function databaseConnect($database=null) {
    if (!is_null($database)) $this->database = $database;
    if (!mysqli_select_db($this->getLink(), $this->database)) {
        return FALSE;
    }
    return TRUE;
}

public function getLink() {
    return $this->link;
}

public function ping() {
    if (mysqli_ping($this->link)) {
        return TRUE;
    }
    return FALSE;
}

public function set($param, $value) {
    $this->$param = $value;
}

public function get($param) {
    return $this->$param;
}

}

$db = new DB;
$link = $db->getLink();
$res = mysqli_query($link, 'SELECT TITLE, DESCRIPTION FROM PRO_CONTENT ORDER BY DATE ASC LIMIT 3');
$content=array();
while ($row = mysqli_fetch_assoc($res)) {
    $content[] = $row;
}

var_dump($content);