数据库类设计 [英] Database class design
问题描述
我正在创建一个具有各种类的Web应用程序,用于处理用户,Smarty模板控件等.
I'm creating a web app with various classes for things like the user, Smarty template control, etc.
我已经有了一个很好的数据库类,但是我担心它的性能.
I already have a database class which is all well and good, but I'm concerned about the performance of it.
当前,在另一个类中,我正在执行$this->db = new DB()
以创建本地数据库实例,但是,每次创建新的DB()
实例时,数据库类的__construct()
函数都会创建与MySQL服务器的新连接,这显然不合情理.这意味着我所有使用数据库类的不同类的每个实例都与服务器建立了连接.我没有大量的类,但是每页加载只需要一个类.
Currently, in another class, I'm doing $this->db = new DB()
to create a local database instance, however the database class's __construct()
function creates a new connection to the MySQL server every time I make a new DB()
instance, which is obviously less than sensible. This means that each instance of all my different classes that uses the database class makes a connection to the server. I don't have a vast amount of classes, but I only want one per page load.
这是我目前所拥有的精简样本:
This is a stripped down sample of what I have at the moment:
// Database class used by multiple other classes
class DB {
private $dbh;
function __construct() {
$this->dbh = // PDO connection here
}
public function query($str) {
// Do a query
}
}
// Example class User
class User {
private $db; // Stores local instance of DB class.
function __construct() {
$this->db = new DB(); // Makes a new connection in DB::__construct()
}
public function login() {
$this->db->query('SELECT * FROM users');
}
}
我正在寻找这样做的最佳"或最常见的做法.我不想为每个页面加载建立10个独立的连接.
I'm looking for the "best" or most common practice of doing this. I don't want to make 10-ish separate connections for each page load.
我想知道在我的应用程序中使用和管理数据库类的最佳方法是什么.我的四个想法是:
I want to know what the best way of using and managing a DB class in my application. My four thoughts are these:
- 使用与MySQL服务器的持久连接是否可以为我解决这个多重连接问题?
- 是否应该使用静态工厂类并返回数据库实例而不是使用
new DB()
? - 使用完全静态的类并在每次引用它时都执行
DB::query()
(例如)是正确的解决方案吗? - 我经常在另一个类中使用多个类(因此我们可能有Folders类,需要User,DB和Smarty类).以某种方式
extend
每个课程都是惯例吗?
- Would using a persistent connection to the MySQL server solve this multiple connection issue for me?
- Should I use a static factory class and return a DB instance instead of using
new DB()
? - Is the proper solution to use an entirely static class and just do
DB::query()
(for example) every time I reference it? - I often use multiple classes in another (so we might have class Folders which requires classes User, DB and Smarty). Is it general practice to
extend
each class somehow?
推荐答案
如果将保持连接的变量设为静态,则可以检查是否已建立连接.在类的所有实例中,静态变量都相同,因此您可以创建100个都使用相同连接的实例.您只需要静态引用它:self :: $ dbh而不是$ this-> dbh.
If you make the variable holding the connection static, then you can check if you already established a connection. Static variables are the same across all instances of the class, so you can create 100 instances that all use the same connection. You just need to reference it statically: self::$dbh instead of $this->dbh.
class DB {
private static $dbh = null;
function __construct() {
if ( is_null(self::$dbh) ) {
self::$dbh = // PDO connection here
}
}
}
这篇关于数据库类设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!