php 单例数据库连接,这段代码是不好的做法吗?
我正在尝试创建一个简单易用的单例类来连接到 mysql 数据库并执行查询,代码工作正常并且我没有遇到任何问题,但由于我是 OOP 新手,我想知道这是否是否是不好的做法。
这是该类
class Database {
private $databaseName = 'dbname';
private $host = 'localhost';
private $user = 'user';
private $password = 'pass';
private static $instance; //store the single instance of the database
private function __construct(){
//This will load only once regardless of how many times the class is called
$connection = mysql_connect($this->host, $this->user, $this->password) or die (mysql_error());
$db = mysql_select_db($this->databaseName, $connection) or die(mysql_error());
echo 'DB initiated<br>';
}
//this function makes sure there's only 1 instance of the Database class
public static function getInstance(){
if(!self::$instance){
self::$instance = new Database();
}
return self::$instance;
}
public function connect() {
//db connection
}
public function query($query) {
//queries
$sql = mysql_query($query) or die(mysql_error());
return $sql;
}
public function numrows($query) {
//count number of rows
$sql = $this->query($query);
return mysql_num_rows($sql);
}
}
//Intantiate the class
$database = Database::getInstance();
,当我想使用该类时,我会这样做:
$query = "SELECT * FROM registrations";
echo $database->numrows($query);
$sql = $database->query($query);
I'm trying to create a simple to use singleton class to connect to mysql database and do queries, the code works fine and i haven't had any problems with it, but since I'm new to OOP I'm wondering whether this is bad practice or not.
Here's the class
class Database {
private $databaseName = 'dbname';
private $host = 'localhost';
private $user = 'user';
private $password = 'pass';
private static $instance; //store the single instance of the database
private function __construct(){
//This will load only once regardless of how many times the class is called
$connection = mysql_connect($this->host, $this->user, $this->password) or die (mysql_error());
$db = mysql_select_db($this->databaseName, $connection) or die(mysql_error());
echo 'DB initiated<br>';
}
//this function makes sure there's only 1 instance of the Database class
public static function getInstance(){
if(!self::$instance){
self::$instance = new Database();
}
return self::$instance;
}
public function connect() {
//db connection
}
public function query($query) {
//queries
$sql = mysql_query($query) or die(mysql_error());
return $sql;
}
public function numrows($query) {
//count number of rows
$sql = $this->query($query);
return mysql_num_rows($sql);
}
}
//Intantiate the class
$database = Database::getInstance();
and when i want to use the class I would do:
$query = "SELECT * FROM registrations";
echo $database->numrows($query);
$sql = $database->query($query);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
单身人士是个坏消息。
您最好查看 依赖注入,因为它解决了上述问题。
Singletons are bad news.
You're better off looking at dependency-injection instead, as it resolves the above issues.
我认为单例对于连接管理器来说是可以的,但对于连接本身来说就不行了。
您永远不知道何时需要为开发的特定部分建立额外的连接。假设您突然需要添加与远程数据库的同步。
连接管理器(可以管理多个连接)可以是单例。连接本身;不。
您的连接管理器还应该能够加载“驱动程序”,以便您能够实例化 MySQL 连接,并且当您需要 msSQL、sqlite 或其他任何东西时,您可以添加所需的驱动程序。
I think a singleton can be OK for a connection manager, but not for a connexion itself.
You never know when you'll need to have an extra connection for a specific part of your development. Let say you suddently need to add a synchronisation with a remote database.
A connection manager (who can manage multiple connection) can be a singleton. A connection itself; no.
You connection manager should also be able to load "Drivers", so that you'll be able to instanciate a MySQL connection and the day you need msSQL, sqLite or anything else, you're able to add the needed drivers.
这种模式很好,因为单例仅适用于当前用户会话。这个决定实际上取决于您的优先事项。如果您希望为用户提供更快的性能,那么您希望每个用户允许更多的数据库连接,但如果您想限制数据库受到攻击的程度,那么单例为您提供了一个很好的中间道路。
This pattern will be fine because the singleton will only apply to the current user session. The decision really comes down to what your priority is. If you want faster performance for the user then you want to allow more database connections per user, but if you want to limit how hard your database gets hit then the singleton gives you a good middle of the road.
我听到的关于 PHP 中 Singleton 设计模式的唯一积极的论点来自一位开发人员,他结合 Memcached 对象实现了 Singleton 数据库连接。我实际上没有机会查看代码和性能,但他能够提出连贯的论点。
就我个人而言,我不认为单例设计模式与 PHP 非常相关,无论如何 PHP 基本上都是无状态的(正如之前指出的,每个请求都会有一个单例)。
The only positive argument I've heard for the Singleton design pattern in PHP was from a developer who implemented a Singleton database connection in conjunction with a Memcached object. I didn't actually get a chance to look at the code and performance but he was able to put forward a coherent argument.
Personally I don't believe that the Singleton design pattern is very relevant to PHP, which is largely stateless anyway (as pointed out before each request will have a singleton).