PHP:正确使用单例模式吗?
我尝试学习 PHP OOP,并对如何创建一个全局数据库类在我的项目中使用进行了一些研究。据我所知,最合适的可用模式是单例,它可以确保始终只存在一个数据库连接。然而,由于这是我第一次使用单例模式,我不确定我是否做对了。
这是一个合适的单身人士吗?这段代码只能确保一个数据库连接吗?有什么方法可以测试这个吗? (授人以渔,终生有食……)
我使用红豆作为我的 ORM,以下是我的简单设置方式:
require_once PLUGINPATH.'rb.php';
$redbean= R::setup("mysql:host=192.168.1.1;dbname=myDatabase",'username','password');
我根据此源制作了以下脚本,作为我自己的单例数据库类;
class database {
private $connection = null;
private function __construct(){
require_once PLUGINPATH.'rb.php';
$this->connection = R::setup("mysql:host=192.168.1.1;dbname=myDatabase",'username','password');
}
public static function get() {
static $db = null;
if ( $db === null )
$db = new database();
return $db;
}
public function connection() {
return $this->connection;
}
}
谢谢!
I've trying to learn PHP OOP and have made some research on how to make a global database class to use around in my project. From what I've seen the most appropriate pattern available is a singleton which would ensure that only one database connection are present at all times. However as this is my first time working with the Singleton pattern, I am not sure that I have made it right.
Is this a proper singleton? Will this code ensure one database connection only? Is there any way I can test this? (Learn a man to fish and he will have food for the rest of his life...)
I am using redbean as my ORM and here's how I set it up plainly:
require_once PLUGINPATH.'rb.php';
$redbean= R::setup("mysql:host=192.168.1.1;dbname=myDatabase",'username','password');
I've made the following script based upon this source, as my own singleton Database class;
class database {
private $connection = null;
private function __construct(){
require_once PLUGINPATH.'rb.php';
$this->connection = R::setup("mysql:host=192.168.1.1;dbname=myDatabase",'username','password');
}
public static function get() {
static $db = null;
if ( $db === null )
$db = new database();
return $db;
}
public function connection() {
return $this->connection;
}
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实例变量必须是类的静态成员。我还没有测试过这段代码,但它应该可以工作。连接可能也应该是静态的。使用此代码,您将拥有数据库类的一个实例和连接的一个实例。可以在连接不静态的情况下执行此操作,但使其静态将确保只有一个连接实例。我还将类的字面名称更改为 php 魔术常量。这使您的代码更易于维护。如果您以后更改类的名称,则不必在代码中查找旧类名称的所有文字实例。现在看来这似乎有点矫枉过正,但相信我,当你从事越来越复杂的项目时,你会欣赏它的价值。
The instance variable needs to be a static member of the class. I haven't tested this code, but it should work. Connection should probably be static too. With this code, you will have one instance of your database class, and one instance of the connection. It is possible to do this without connection being static, but making it static will ensure there is only one connection instance. I also changed the literal names of the class to php magic constants. This make your code more maintainable. If you change the name of your class down the road, you won't have to go and find all of the literal instances of the old class name in your code. This may seem like overkill now, but trust me, as you work on more and more complex projects, you will appreciate the value.
你是单身人士几乎是正确的。
私有成员(没有双关语)
$connection
也需要是静态的。您也可以选择以下内容:You're singleton is almost correct.
The private member (no pun intended)
$connection
needs to be static as well. You might go with the following too: