久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

用于數據庫連接的全局或單例?

Global or Singleton for database connection?(用于數據庫連接的全局或單例?)
本文介紹了用于數據庫連接的全局或單例?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在 PHP 中使用單例而不是全局進行數據庫連接有什么好處?我覺得使用單例而不是全局會使代碼不必要地復雜.

What is the benefit of using singleton instead of global for database connections in PHP? I feel using singleton instead of global makes the code unnecessarily complex.

$conn = new PDO(...);

function getSomething()
{
    global $conn;
    .
    .
    .
}

單例代碼

class DB_Instance
{
    private static $db;

    public static function getDBO()
    {
        if (!self::$db)
            self::$db = new PDO(...);

        return self::$db;
    }
}

function getSomething()
{
    $conn = DB_Instance::getDBO();
    .
    .
    .
}

如果有比全局或單例更好的初始化數據庫連接的方法,請提及并描述它相對于全局或單例的優勢.

If there's a better way of initializing database connection other than global or singleton, please mention it and describe the advantages it have over global or singleton.

推薦答案

我知道這已經過時了,但 Dr8k 的回答幾乎就在那里.

I know this is old, but Dr8k's answer was almost there.

當您考慮編寫一段代碼時,假設它會發生變化.這并不意味著您假設它會在未來的某個時間點對它進行各種更改,而是會進行某種形式的更改.

When you are considering writing a piece of code, assume it's going to change. That doesn't mean that you're assuming the kinds of changes it will have hoisted upon it at some point in the future, but rather that some form of change will be made.

將其作為目標可以減輕未來進行更改的痛苦:全局是危險的,因為很難在一個地方進行管理.如果我希望將來知道該數據庫連接上下文怎么辦?如果我希望它每使用 5 次就關閉并重新打開,該怎么辦?如果我決定為了擴展我的應用程序我想使用 10 個連接池怎么辦?還是可配置的連接數?

Make it a goal mitigate the pain of making changes in the future: a global is dangerous because it's hard to manage in a single spot. What if I want to make that database connection context aware in the future? What if I want it to close and reopen itself every 5th time it was used. What if I decide that in the interest of scaling my app I want to use a pool of 10 connections? Or a configurable number of connections?

單例工廠為您提供了這種靈活性.我用很少的額外復雜性來設置它,并且獲得的不僅僅是訪問相同的連接;我獲得了稍后以簡單方式更改該連接傳遞給我的方式的能力.

A singleton factory gives you that flexibility. I set it up with very little extra complexity and gain more than just access to the same connection; I gain the ability to change how that connection is passed to me later on in a simple manner.

請注意,我說的是 singleton factory 而不是簡單的 singleton.單身人士和全球人士之間幾乎沒有什么區別,真的.正因為如此,沒有理由使用單例連接:當您可以創建一個常規的全局連接時,為什么還要花時間設置它?

Note that I say singleton factory as opposed to simply singleton. There's precious little difference between a singleton and a global, true. And because of that, there's no reason to have a singleton connection: why would you spend the time setting that up when you could create a regular global instead?

工廠給你的是為什么要獲得聯系,以及一個單獨的地點來決定你將獲得什么聯系(或聯系).

What a factory gets you is a why to get connections, and a separate spot to decide what connections (or connection) you're going to get.

class ConnectionFactory
{
    private static $factory;
    private $db;

    public static function getFactory()
    {
        if (!self::$factory)
            self::$factory = new ConnectionFactory(...);
        return self::$factory;
    }

    public function getConnection() {
        if (!$this->db)
            $this->db = new PDO(...);
        return $this->db;
    }
}

function getSomething()
{
    $conn = ConnectionFactory::getFactory()->getConnection();
    .
    .
    .
}

然后,在 6 個月內,當您的應用程序非常出名并被挖出和斜線標記并且您決定需要多個連接時,您所要做的就是在 getConnection() 方法中實現一些池化.或者,如果您決定需要一個實現 SQL 日志記錄的包裝器,您可以傳遞一個 PDO 子類.或者,如果您決定在每次調用時都需要一個新連接,則可以這樣做.它是靈活的,而不是僵硬的.

Then, in 6 months when your app is super famous and getting dugg and slashdotted and you decide you need more than a single connection, all you have to do is implement some pooling in the getConnection() method. Or if you decide that you want a wrapper that implements SQL logging, you can pass a PDO subclass. Or if you decide you want a new connection on every invocation, you can do do that. It's flexible, instead of rigid.

16 行代碼,包括大括號,這將為您節省數小時、數小時和數小時的重構時間,以實現極其相似的內容.

16 lines of code, including braces, which will save you hours and hours and hours of refactoring to something eerily similar down the line.

請注意,我不考慮這種功能蠕變",因為我在第一輪中沒有進行任何功能實現.它是Future Creep"的邊界線,但在某些時候,今天為明天編碼"的想法總是對我來說是件壞事.

Note that I don't consider this "Feature Creep" because I'm not doing any feature implementation in the first go round. It's border line "Future Creep", but at some point, the idea that "coding for tomorrow today" is always a bad thing doesn't jive for me.

這篇關于用于數據庫連接的全局或單例?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作為類名,因為它是保留的 Cake 2.2.x)
Session is lost after an OAuth redirect(OAuth 重定向后會話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁排序)
CakePHP Shared core for multiple apps(CakePHP 多個應用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯誤:允許的內存大小為 134217728 字節已用盡(嘗試分配 87 字節))
主站蜘蛛池模板: 99福利视频| 欧美日韩成人在线 | 国产精品免费观看 | 日本不卡免费新一二三区 | 亚洲精品短视频 | 中文字幕一级 | 一区二区三区免费看 | 国产精品久久久久久久久久免费 | a在线免费观看 | 亚洲免费毛片 | 国产午夜精品久久久久免费视高清 | 国产一级一级毛片 | 日本a在线 | 欧美黑人国产人伦爽爽爽 | 日韩一区二区在线观看视频 | 在线观看黄免费 | 亚欧洲精品在线视频免费观看 | 国产片侵犯亲女视频播放 | 日韩中文在线观看 | 免费一区二区三区 | 欧美久久一区 | 成人小视频在线观看 | 亚洲三级av | 国产免费一区二区 | 国精产品一品二品国精在线观看 | 看特级黄色片 | 国产精品久久久久久久久久免费看 | 国产一级片 | 午夜99 | 精品视频国产 | 日本激情一区二区 | 欧美一a一片一级一片 | 国产精品久久免费观看 | 91就要激情 | 国产精品国产精品国产专区不片 | 91久久国产综合久久91精品网站 | www.五月天婷婷 | 精品国产18久久久久久二百 | 99精品久久 | 亚洲网站免费看 | 亚洲精品99 |