通过PHP类的MySQL数据库连接的单个实例 [英] Single instance of a MySQL database connection via PHP Class

查看:111
本文介绍了通过PHP类的MySQL数据库连接的单个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据库类.我的想法是,这将检查该类的现有实例并返回该实例,而不是创建新的数据库连接.

I have the following database class. My thinking was that this will check for an existing instance of the class and return that rather than create a new database connection.

运行代码时,它会创建一个连接.当我刷新页面时,将创建另一个连接(选中的MySQL连接).

When I run the code it creates a connection. When I refresh the page another connection is created (checked MySQL connections).

我的想法不正确吗?对于新手问题,使用OOP还是很新的道歉!

Is my thinking incorrect? Fairly new to using OOP so apologies for the newbie question!

任何帮助或朝着正确方向的指示都将不胜感激.

Any help or pointers in the right direction would be appreciated.

非常感谢.

<?php
class Db
{
    private $_connection;
    private static $_instance; 
    private $_host = 'localhost';
    private $_username = 'root';
    private $_password = 'password';
    private $_database = 'test';

    public static function getInstance()
    {
        if (!self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    private function __construct()
    {
        try {
            $this->_connection  = new PDO("mysql:host=$this->_host;dbname=$this->_database", $this->_username, $this->_password); 
            echo 'Connected to database';
        } catch (PDOException $e) {
            echo $e->getMessage();
        }
    }

    private function __clone()
    {
    }

    public function getConnection()
    {
    return $this->_connection;
    }
}

$db = Db::getInstance();

推荐答案

PHP是"没有任何共享​​环境.根据单个,但是它被隔离为一个请求-响应周期.这意味着,如果您在单个请求中调用Db::getInstance() 10次,您将获得10个对同一对象的引用,但是在单独请求中的单个调用将创建并返回一个不同的对象.

PHP is a "shared nothing" environment. Each request handled by a PHP application is isolated from all other requests either as a separate thread or a separate process depending on the server api (SAPI) being used. The class you have designed is a singleton but it is isolated to a single request-response cycle. This means that if you call Db::getInstance() 10 times during a single request you will get 10 references to the same object, but a single call in a separate request will create and return a distinct object.

您可以在服务器或应用程序端使用某种类型的连接池,以减少与后端数据库服务器建立的并发连接数. PHP的 PDO 抽象可通过

You can use some type of connection pooling, either on the server or application side, to reduce the number of concurrent connections made to your backend database server. PHP's PDO abstraction enables application side connection pooling via the PDO::ATTR_PERSISTENT connection driver option. These pooled connections are cached in the PHP parent process rather than the worker thread/process that handles a request and subsequently reused. The exact number of connections that will be opened and how they are shared is variable depending on your SAPI and underlying database type.

这篇关于通过PHP类的MySQL数据库连接的单个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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