0

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

$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 {


}
4

2 回答 2

0

通常这个DB类也只是保持连接。做一个单独的连接类是过度设计的。

我更担心的是 API 而DB不是内部。

于 2013-08-09T15:07:57.560 回答
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);
于 2013-08-11T01:14:12.600 回答