PDO Singleton Class,我做得对吗?(PDO Singleton Class, am i doing it right?)

我正在开发一个新项目。 在这个项目中,我使用了我第一次使用的pdo单例类,我不知道我是否正确行事。

如果你能给我提示或改进,那会很好。

<?php namespace App\Libraries; class Database { private $_config; private $_instance = null; public function __construct($config) { $this->_config = $config; } public function getInstance() { if ($this->_instance == null) { try { $this->_instance = new \PDO('mysql:host=' . $this->_config['database']['host'] . ';dbname=' . $this->_config['database']['name'] . ';charset=' . $this->_config['database']['charset'], $this->_config['database']['username'], $this->_config['database']['password'], [ \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_EMULATE_PREPARES => false ]); } catch (PDOException $e) { die($e->getMessage()); } } return $this->_instance; } public function __destruct() { $this->_config = null; $this->_instance = null; } } ?>

$config变量只是我的配置文件,我使用了如下类:

$db = new Database($config); $db->getInstance()->query(...);

干杯,很少

I'm working on a new project. On this project I've used a pdo singleton class which I am using for the first time and I don't know if I'm doing it right.

Would be nice if you could give me tips or improvements.

<?php namespace App\Libraries; class Database { private $_config; private $_instance = null; public function __construct($config) { $this->_config = $config; } public function getInstance() { if ($this->_instance == null) { try { $this->_instance = new \PDO('mysql:host=' . $this->_config['database']['host'] . ';dbname=' . $this->_config['database']['name'] . ';charset=' . $this->_config['database']['charset'], $this->_config['database']['username'], $this->_config['database']['password'], [ \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_EMULATE_PREPARES => false ]); } catch (PDOException $e) { die($e->getMessage()); } } return $this->_instance; } public function __destruct() { $this->_config = null; $this->_instance = null; } } ?>

The $config variable is just my config file and I've used the class as follows :

$db = new Database($config); $db->getInstance()->query(...);

Cheers, Rarely

最满意答案

在您的情况下, Database不是单例,因为您可以创建此类的多个实例。 如果需要单例,请使用静态属性作为save实例。 并删除构造函数(将其声明为私有)。

<?php namespace App\Libraries; class Database { private static $_instance = null; private function __construct() { } public static function getInstance() { $config = // Implement reading of configuration if ($this::$_instance == null) { try { $this::$_instance = new \PDO('mysql:host=' . $config['database']['host'] . ';dbname=' . $config['database']['name'] . ';charset=' . $config['database']['charset'], $config['database']['username'], $config['database']['password'], [ \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_EMULATE_PREPARES => false ]); } catch (PDOException $e) { die($e->getMessage()); } } return $this::$_instance; } } ?>

比你将使用这样的Database :

$db = Database::getInstance()->query(...);

Database is not singleton in your case, because you can create several instances of this class. If you need singleton, use static property for save instance. And remove constructor (declare it as private).

<?php namespace App\Libraries; class Database { private static $_instance = null; private function __construct() { } public static function getInstance() { $config = // Implement reading of configuration if ($this::$_instance == null) { try { $this::$_instance = new \PDO('mysql:host=' . $config['database']['host'] . ';dbname=' . $config['database']['name'] . ';charset=' . $config['database']['charset'], $config['database']['username'], $config['database']['password'], [ \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_EMULATE_PREPARES => false ]); } catch (PDOException $e) { die($e->getMessage()); } } return $this::$_instance; } } ?>

Than you will use Database like this:

$db = Database::getInstance()->query(...);

更多推荐