Singleton PHP - 数据库处理程序 [英] Singleton PHP - database handler

查看:81
本文介绍了Singleton PHP - 数据库处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在阅读有关单身人士模式。当阅读它的技术方面时,它似乎非常适合于管理数据库处理程序等。但是在阅读更广泛的资源后,开发人员社区似乎并不喜欢这种模式。

I have been reading a bit lately about the singleton pattern. When readin the technical aspects of it, it appears to be ideally suited to managing a database handler or the likes. But after reading wider resources it appears that the developer community really does not favour the pattern.

我正在努力寻找一个更好的解决方案来解决这个问题 - 即只有一个处理程序可以一次初始化 - 所以为什么模式如此糟糕?是否过度使用或仅仅是从根本上有缺陷?

I am struggling to find a better solution to such a problem - i.e. only a single handler can be initialised at a time - so why is the pattern so bad? Is it overused or is it just fundamentally flawed?

Php是我使用的语言。

Php is the language that I am using.

推荐答案

单身人士是荣耀的全球变量。设计模式是为全球变量困难或不可能的语言创建的,或者被认为是坏习惯的语言。 (实际上,大多数常见的设计模式都是针对限制性语言设计的,其中很多都是其他语言中不必要的。)

Singletons are glorified global variables. The design pattern was created for languages in which global variables are difficult or impossible, or where they are considered bad practice. (In fact, most of the common design patterns are designed for restrictive languages. A great number of them are simply unnecessary in other languages.)

PHP具有全局变量。 PHP全局变量通常是一个不好的做法,但是如果你需要使用它们,它们确实存在。

PHP has global variables. PHP global variables are usually a bad practice, but they do exist if you need to use them.

但是,有几个原因你想要一个Singleton在PHP

However, there are a few reasons you'd want a Singleton in PHP.

当调用 getInstance (返回单个实例的方法的规范名称我可以在脚本的任何一点做出单身人士。直到那一点,对象不需要存在。如果对象是全局变量,或者它必须已经存在,或者试图引用对象的代码首先需要实例化。事实上,在任何可以使用它的地方,它都需要被正确地实例化。通过在 getInstance 中集中创建单个对象,您不必在每次需要引用对象时创建复制和粘贴样板。

Singletons are useful when the call to getInstance (the canonical name for the method that returns the single instance of the Singleton) might me made at any point in the script. Up until that point, the object doesn't need to exist. If the object was a global variable instead, either it would have to already exist, or the code trying to reference the object would first need to instantiate it. In fact, anywhere that it could be used, it would need to be instantiated correctly. By centralizing the creation of the single object in getInstance, you avoid having to create copy-and-paste boilerplate every time you need to reference the object.

数据库对象通常在请求生命周期的早期创建,这样就可以浪费Singleton的特定利益。

Database objects usually get created very early on in the request lifetime, so that specific benefit of Singleton-ness would be wasted.

Singleton还有其他替代方法可以以其他方式完成工作。一个例子是依赖注入,这是一个传统外部对象的花哨术语,一个新的对象将依赖于(如数据库句柄)在构建时对象。但是,这可能是复杂或烦人的。

There are other alternatives to Singleton that can get the job done in other ways. One example is dependency injection, a fancy term for passing external objects that a new object would depend on (such as a database handle) to the object at construction time. However, this can be complicated or annoying. Doing it right might involve injecting a lot of the same objects every single time.

另一个选择是注册表模式,它实际上是否则是全局变量的容器。如果你不喜欢全局变量,但不介意它们被有效地命名,这将是一个你想要的解决方案。

Another alternative is the Registry pattern, which is effectively a container for things that would otherwise be globals. If you don't like global variables, but don't mind them being effectively namespaced, this would be a solution that you'd like.

最后,选择一种方法来做到这一点,并坚持使用这种方式遍及您的代码库。就个人而言,我是数据库对象是全球的粉丝。

In the end, pick one way to do it, and stick with that one way throughout your codebase. Personally, I'm a fan of the database object being a global.

这篇关于Singleton PHP - 数据库处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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