php单例数据库连接,这个代码坏的做法吗? [英] php singleton database connection, is this code bad practice?
本文介绍了php单例数据库连接,这个代码坏的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个简单的使用singleton类连接到mysql数据库和查询,代码工作正常,我没有任何问题,但因为我是新的OOP我
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.
以下是类
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);
推荐答案
单身人士是坏消息。
- 他们将全局状态引入程序。大多数程序员应该熟悉为什么全局状态是坏的。
- 他们在单例和使用它的任何类之间引入紧耦合。这意味着你不能重用这个类,而不重用单例。
- 他们对依赖于单例问题的类进行单元测试,因为你不能用模拟代替单例。
- 他们鼓励一种编码风格,其中类试图解决自己的依赖。这是坏的,因为它可以减少类的依赖关系的清晰度。
- PHP有一个Share Nothing架构,这意味着PHP单例实际上不是单例,可以有多个实例在任何一个时间活动(每个打开的请求一个)。
- 如果您稍后突然发现您实际上需要多个由单身人士提供的资源,会发生什么?这是比你想象的更常见的情况。
- They introduce global state into a program. Most programmers should be familiar with why global state is bad.
- They introduce tight coupling between the singleton and any class that uses it. This means you can't reuse the classes in question without reusing the singleton too.
- They make unit testing of classes that depend on the singleton problematic because you can't easily replace the singleton with a mock.
- They encourage a coding style where classes attempt to resolve their own dependencies. This is bad because it can reduce clarity regarding what dependencies the class has.
- PHP has a Share Nothing Architecture, meaning that PHP singletons aren't really singletons at all, there can be multiple instances alive at any one time (one per open request).
- What happens if you suddenly discover at some later date that you actually need more than one of the resource that's being provided by the singleton? It's a more common scenario than you might think
你最好看看依赖注入,因为它解决了上述问题。
You're better off looking at dependency-injection instead, as it resolves the above issues.
这篇关于php单例数据库连接,这个代码坏的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文