如何静态和单例类工作(数据库) [英] How static vs singleton classes work (databases)

查看:135
本文介绍了如何静态和单例类工作(数据库)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对单例模型与静态模型如何工作的数据库连接感到困惑。我的朋友创建了一个静态类,并显示了我,但它没有任何意义,它是如何是静态的。我理解单例方法如何创建一个数据库连接,但我不能确定它是否符合我的目标。



我想做的主要事情是cut关闭打开到MYSQL的连接数。我有一个类有一个函数,经常调用数据库,并没有理由每当有人请求的东西,需要数据库时,它做一个新的连接。有人可以提供一个小的示例类,使用单例或静态方法(以正确的方法)连接到数据库并显示一个小的示例查询吗?我非常感谢。



哦,我使用PHP 5.3 :)请随时询问更多细节。

 类数据库{

protected static $ _dbh;
const HOST ='localhost';
const DATABASE ='dbname';
const USERNAME ='username';
const PASSWORD ='password';

//将构造函数声明为private,以避免直接实例化。
private function __construct(){}

//通过getInstance方法访问数据库对象。
public static function getInstance(){
if(!isset($ _ dbh)){
#Connection String。
self :: $ _ dbh = new PDO('mysql:host ='。self :: HOST。'; dbname ='。self :: DATABASE,self :: USERNAME,self :: PASSWORD);
self :: $ _ dbh-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
}
return self :: $ _ dbh;
}
}

现在如果我必须使用该类在应用程序中我会很简单这样做。

  require_once('database.php'); 
$ dbh = Database :: getInstance();
$ sth = $ dbh-> query('SELECT * FROM sometable');
$ result = $ sth-> fetchAll(PDO :: FETCH_ASSOC);

调用 Database :: getInstance(); 使用静态方法。这基本上做的是它限制你直接实例化对象,通过声明构造函数作为私有,而是检查对象是否已经instanitated。如果为true,则返回已实例化的对象。否则创建新的并返回新创建的对象。这确保同一数据库连接可以通过应用程序重用。


I am confused on how a singleton model vs a static model works for database connections. My friend created a "static" class and showed me it but it did not make any sense on how it was static. I kind of understand the singleton method of how to create a database connection, but i'm not sure it meets my goal.

The main thing I want to do is cut down on the number of connections opened to MYSQL. I have a class with a function that calls the database quiet frequently, and there is no reason for it to make a new connection each time someone requests something that requires the database. Could someone provide a small example class for doing this with the singleton or the static method (whichever is the correct approach) that connects to the database and shows a small sample query? I would greatly appreciate it.

Oh yeah, I am using PHP 5.3 :) Please feel free to ask for additional details.

解决方案

Consider the following example which uses the singleton design pattern to access the instance of database object.(purpose of this is to reuse the same connection again and again through out the application)

class Database {

    protected static $_dbh;
    const HOST = 'localhost';
    const DATABASE = 'dbname';
    const USERNAME = 'username';
    const PASSWORD = 'password';

    //declare the constructor as private to avoid direct instantiation.   
    private function __construct() { }

    //access the database object through the getInstance method.
    public static function getInstance() {
        if(!isset($_dbh)) {
            #Connection String.
            self::$_dbh = new PDO('mysql:host='.self::HOST.';dbname='.self::DATABASE,self::USERNAME,self::PASSWORD);
            self::$_dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        return self::$_dbh;
    }
}

now if i have to make use of the class anywhere in the application i would simple do it like this.

require_once('database.php');
$dbh = Database::getInstance();
$sth = $dbh->query('SELECT * FROM sometable');
$result = $sth->fetchAll(PDO::FETCH_ASSOC);

the call to Database::getInstance(); uses static method. what this basically does is it restricts you from directly instantiating the object by declaring the constructor as private, and instead it checks if the object is already instanitated. if true then return already instantiated object. else create new and return newly created object. this makes sure that same database connection is reused through out the application.

这篇关于如何静态和单例类工作(数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆