简体   繁体   English

如何为所有其他类实现数据库类?

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

I have a large amount of classes for: a main forums class, a forum boards class, and a threads class. 我有很多用于以下方面的类:主论坛类,论坛面板类和线程类。 These can be thought of as models in the context of my website. 在我的网站范围内,可以将它们视为模型。

The setup the previous web developer was using creates a new mysqli connection in each one of these classes and connects on instantiation of the class. 以前的Web开发人员使用的设置在这些类的每一个中创建一个新的mysqli连接,并在该类的实例化时进行连接。 This means that in the threads page there are 3 different connections for forums, threads, and users. 这意味着在“主题”页面中,有3个不同的论坛,主题和用户连接。 This is obviously not ideal and I think not even optimal. 这显然不是理想的,我认为甚至不是最佳的。

I worked on a previous project in which I could simply just pass a new instance of the database class to the class I was using, but this isn't ideal because I need to instantiate multiple classes and passing a database instance to each class would defeat the purpose. 我在以前的项目中工作过,在该项目中我只是可以将数据库类的新实例传递给所使用的类,但这并不理想,因为我需要实例化多个类,并且将数据库实例传递给每个类都会失败目的。

In each of these classes, database calls are made so the database object/instance is needed within each class, without having to instantiate it 3+ separate times. 在这些类的每一个中,都进行数据库调用,因此在每个类中都需要数据库对象/实例,而无需实例化3次以上。

For example, in the threads class you could have: 例如,在线程类中,您可以具有:

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

Does anyone know how I can go about achieving this? 有谁知道我该如何去实现这一目标?

I prefer one database controller that you could get data from as in a factory pattern. 我更喜欢一个数据库控制器,可以像工厂模式那样从中获取数据。 In the example below, you could just create one controller and let your components use it as they need. 在下面的示例中,您可以只创建一个控制器,然后让组件按需使用它。

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

I hope this helps 我希望这有帮助

# 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.

相关问题 如何使用Laravel的Illuminate Database类实现事务? - How can I implement transactions with Laravel's Illuminate Database class? 如何在其他类中运行数据库类方法? - How to run database class methods inside other classes? 如何以适当的方式扩展某些php类,以便从子类访问其他子类的所有公共方法? - How can extend in proper way some php classes for have access from child class to all public methods from other child class? 如何只为字符串操作创建一个类,并在所有其他类中将其用于其他类中重复的特定操作 - How to create a class only for string manipulation and usable in all other classes for specific manipulations that repeats in other classes 如何创建包含几个可以相互通信的类的类? - How do I create a class that contains several classes that can communicate with each other? 如何实例化我的数据库对象,以便在其他类中使用? - How do I instantiate my database object, to be used in other classes? 数据库类连接到我的其他类 - Database class to connect to my other classes 我应该如何在其他课程中使用数据库课程? - How should I use my database class in other class? 如何在其他类中使用数据库连接 - How to use database connection in other classes 如何将所有包含项与类放在一页上? - How can I put all includes on one page with classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM