簡體   English   中英

如何為所有其他類實現數據庫類?

[英]How can I implement a database class for all other classes?

我有很多用於以下方面的類:主論壇類,論壇面板類和線程類。 在我的網站范圍內,可以將它們視為模型。

以前的Web開發人員使用的設置在這些類的每一個中創建一個新的mysqli連接,並在該類的實例化時進行連接。 這意味着在“主題”頁面中,有3個不同的論壇,主題和用戶連接。 這顯然不是理想的,我認為甚至不是最佳的。

我在以前的項目中工作過,在該項目中我只是可以將數據庫類的新實例傳遞給所使用的類,但這並不理想,因為我需要實例化多個類,並且將數據庫實例傳遞給每個類都會失敗目的。

在這些類的每一個中,都進行數據庫調用,因此在每個類中都需要數據庫對象/實例,而無需實例化3次以上。

例如,在線程類中,您可以具有:

function get_threads($board_id) {
    return $this->con->query("some query");
}

有誰知道我該如何去實現這一目標?

我更喜歡一個數據庫控制器,可以像工廠模式那樣從中獲取數據。 在下面的示例中,您可以只創建一個控制器,然后讓組件按需使用它。

ex: $threads = new ThreadClass($mysqlConnection);

我希望這有幫助

# 1. mysql source or "socket" using PDO
class SimpleDatabaseSocket {

    protected static $dbh = false;

    public function connect($database, $user, $pass, $host) {
        $dsn = "mysql:dbname={$database};host={$host}";
        self::$dbh = new PDO($dsn, $user, $pass);
        self::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

}

# 2. always use an interface which defines required logic
interface SimpleDatabaseInterface {

    public function onRunQuery($query);
}

# 3. create the accessible controller for inherited & ancillary logic
class SimpleDatabaseController extends SimpleDatabaseSocket implements SimpleDatabaseInterface {

    private $host = "";
    private $base = "";
    private $usr = "";
    private $pwd = "";
    private $table = "";

    # when we create the controller, you MUST pass the connection params
    public function __construct($host, $usr, $pwd, $base) {
        $this->setUsr($usr);
        $this->setPwd($pwd);
        $this->setBase($base);
        $this->setHost($host);
    }

    # mysql host/ip
    public function setHost($host) {
        $this->host = $host;
    }

    # mysql database
    public function setBase($base) {
        $this->base = $base;
    }

    # mysql username
    public function setUsr($usr) {
        $this->usr = $usr;
    }

    # mysql password
    public function setPwd($pwd) {
        $this->pwd = $pwd;
    }
    # allow this controller to switch tables dynamically.
    public function onChangeTable($table) {
        $this->table = $table;
    }

    # connect to the database
    protected function onConnect() {
        $this->connect($this->base, $this->usr, $this->pwd, $this->host);
    }

    # connects to the DB and runs a custom query
    public function onRunQuery($query) {
        try {
            if (!self::$dbh)
                $this->onConnect();
            $result = self::$dbh->query($query);
            $out = $result->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {

        }
        return $out;
    }

}

# exmaple usage
$mysql = new SimpleDatabaseController("HOSTNAME","USERNAME","PASSWORD","DATABASE");
$result = $mysql->onRunQuery("SELECT * FROM TABLE LIMIT 0,2");
print("<pre>" . print_r($result, true) . "</pre>");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM