简体   繁体   English

PHP OOP:创建数据库类

[英]PHP OOP: Creating database class

I am making my first steps in the OOP world - please bear with me. 我正在面向OOP领域迈出第一步-请忍受。
I know that having many ongoing mySQL connections open at the same time can be fatal for performance, so it should be a good idea to make a database class that takes this into account. 我知道同时打开多个正在进行的mySQL连接可能会降低性能,因此,使数据库类考虑到这一点应该是一个好主意。

Is $this->session->write(...); $this->session->write(...); going to result in a new mySQL connection being opened each time? 每次都会打开一个新的mySQL连接? Or is that up to the "persistent connection" feature in mySQL? 或者这取决于mySQL中的“持久连接”功能?

Here's the code: 这是代码:

abstract class database {
    function __construct() {
       //mysql_connect()
    }
}

class session extends database {
    function write () {
       // mysql_query(--.)
    }
}

mysql_query will create a new connection only if no previous mysql connection has been made. 只有在没有建立先前的mysql连接时,mysql_query才会创建新连接。 Otherwise, it will either use the connection you specify or the last connection opened with mysql_connect. 否则,它将使用您指定的连接或使用mysql_connect打开的最后一个连接。 http://php.net/manual/en/function.mysql-query.php http://php.net/manual/en/function.mysql-query.php

Is session handler some kind of specialized type of database? 会话处理程序是某种特殊类型的数据库吗? It is not , so don't use inheritance (a is a relationship). 不是 ,所以不要使用继承(a 是一种关系)。 Session handler uses database so you should use composition (a has a relationship): 会话处理程序使用数据库,因此您应该使用合成(a 具有关系):

class Session {
    protected $db;

    public function __construct(DB $db) {
        $this->db = $db;
    } 

    public function write() {
        $this->db->query(...);
    }
}


$db = new DB(...);
$session = new Session($db);

Also, don't use mysql_*() functions. 另外,不要使用mysql_*()函数。 Use much more powerful PDO . 使用功能更强大的PDO


Returning to your question... mysql_connect() will be executed every time you create a new Database object. 回到你的问题......每次创建一个新的Database对象时都会执行mysql_connect() However in this case Session is an object of type Database so every time you write new Database() or new Session() the constructor is invoked, therefore mysql_connect() is invoked as well. 但是,在这种情况下, SessionDatabase类型的对象,因此,每次您编写new Database()new Session() ,都会调用构造函数,因此也会调用mysql_connect()

PS. PS。 mysql_connect() function won't create a new connection if specified connection already exists. 如果指定的连接已经存在, mysql_connect()函数将不会创建新的连接。 Check 4th argument of this function to learn more. 检查此功能的第4个参数以了解更多信息。

Why not 为什么不

class database {
    function __construct() {
       //mysql_connect()
    }
    function write() {
       //query the DB
    }
}

I'm not sure of the syntax, I don't do OOP PHP. 我不确定语法,我不做OOP PHP。 Anyway, in your structure above a new connection would be opened for each "session" instance so assuming you only create one instance of "session" you won't be openeing loads of database connections. 无论如何,在上面的结构中,将为每个“会话”实例打开一个新的连接,因此,假设您仅创建“会话”的一个实例,则不会打开数据库连接的负载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM