PDO + Singleton:为什么要使用它? [英] PDO + Singleton : Why using it?

查看:88
本文介绍了PDO + Singleton:为什么要使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上阅读到许多开发人员正在使用单例模式进行PDO连接. [哪里?]

I read on the web that many developers are using singleton pattern for their PDO connections. [where?]

之前(在VB.Net中)我一直在使用单例模式,并且我很确定,它仅在必须调用一个实例两次或更多次(缺少.NET代码的代码示例)时才有用.

I've been using singleton patterns before (in VB.Net), and I'm quite sure that it's only useful when you have to call an instance twice or more [code example of .NET code missing].

我目前正在一个电子商务网站上,我想知道在我的PDO连接上使用单例模式是否真的有用,因为我的体系结构是通过每个脚本仅需要一个连接的方式来制作的[代码示例,您将使用这种单例丢失].

I'm currently working on an e-commerce website, I'd like to know if it's really useful to use a singleton pattern on my PDO connections since my architecture is made in a way that each script only needs one connection [code example where you would use such a singleton missing].

推荐答案

针对此问题(甚至答案)引发的评论讨论" ,对我的看法和普遍的共识(不要忽略设备/服务器的负载,这意味着加倍的工时,而可维护性比速度更重要,除了一些特定的情况).

In response to the "comment-discussion" that this question (and perhaps answer) has ignited, a recap of my opinion, and the general consensus (don't skimp on equipment/server load, when that means doubling the man-hours and maintainability is more important than speed, save for a few specific cases).

您说您是讨厌的框架,而使用未开发框架的开发人员会出售维护合同.这是如何工作的?熟悉FW的任何人都可以维护使用开源的,已记录的FW的代码.您可以维护您的代码.
此外,PHP框架(至少是主要框架)是 OPEN SOURCE .我知道,从VB.NET背景开始,您可能会怀疑有这个问题,但实际上没有.他们都有github repo,可以自由复制和编辑源代码(根据许可证,总的来说,这是相当自由的).基本上,您拥有完全的控制权.
而且不要以为这是所有二级代码:Apple(包括其OSX内核的主要部分),Microsoft,Google ...它们都是使用开源软件和 frameworks的 all .不仅是因为它易于开发,开发速度更快,还因为它是经过验证的技术的概念.

You say you hate frameworks and that devs who use frameworks they didn't develop themselves sell maintainance contracts. How exactly does that work? Code using an open source, documented FW can be maintained by anyone who knows the FW. Your code can be maintained by you.
Besides, PHP frameworks are (well, the main ones at least) OPEN SOURCE. I know that, coming from a VB.NET background, you might suspect there is a catch to this, but there really isn't. They all have github repo's, the source is freely available to copy, and edit (in accordance to the license, which, on the whole is quite liberal anyway). Basically, you have full control.
And don't go thinking it's all second-grade code: Apple (including major parts of their OSX kernel), Microsoft, Google... they are all using open-source software and frameworks. Not only because it's easy, and faster to develop, but also because of the notion of proven technology.

此外:性能是您的目标,并且-如果您讨厌框架-,为什么不考虑学习组装?这样,您就可以避免让笨拙的编译器将您的代码编译为可能不如以前那么高效的代码.
当然,您无需为此使用IDE,您可以选择Emacs或Butterfly,对吗?

Besides: performance is your goal, and -given that you loathe frameworks-, why not consider learning assembly? That way, you can avoid having an idiotic compiler compile your code to something that isn't as efficient as, perhaps, it could have been.
And of course, you don't use an IDE for this, you'd either go for Emacs or butterflies, right?


(来源: andreas-wilm.com )


(source: andreas-wilm.com)

一种替代方法是使用提供高级缓存功能 ...

An alternative approach would be to use a framework that offers advanced caching featrures...

在编写代码时,可维护性对您来说最重要:

Maintainability should be at the most important thing to you, while writing code:

'调试的难度是一开始编写代码的两倍.因此,如果您尽可能聪明地编写代码,那么就定义而言,您就不够聪明,无法对其进行调试.' — Brian Kernighan

'Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.' — Brian Kernighan

服务器是僵尸奴隶:它们不会抱怨,鞭打它们也不是非法的.尽管有些项目经理似乎忘记了这一点,但是开发人员既不是僵尸也不是奴隶:他们不能连续工作两周,鞭打开发人员 仍然是非法的.甚至史蒂夫·鲍尔默(Steve Ballmer)都知道... 开发人员,开发人员,开发人员. Microsoft的Server OS,MSSQL以及几乎所有的软件都是资源密集型的,但要研究他们的服务器OS.许多功能只能减少系统所需的维护时间.我不是MS迷,但我完全理解这种决定.他们无法在价格上击败Linux(因为它是免费的),但是他们可以通过出售系统来吸引公司,这将使系统管理员的成本降低一半. (不过,ATM尚未实现,而IMO则从未实现)

Servers are zombie-slaves: they don't complain, and it's not illegal to whip them. Developers, though some project-managers seem to forget this, are neither zombies or slaves: they can't work 2 weeks on end, and whipping devs is illegal, still. Even Steve Ballmer knows that... Developers, Developers, Developers. Microsoft's Server OS's are resource intensive, as is MSSQL, and pretty much all their software, but look into their server OS. There are many features that only serve to reduce the maintainance hours your system requires. I'm no MS fan-boy, but I fully understand that descision. They can't beat Linux on price ('cause it's free), but they can lure in companies by selling a system, that will cut their sys-admin costs in half. (though, ATM, they haven't acchieved this, and IMO, they never will)

此问题是许多重复问题之一,不适合SO,因此将关闭.但是,在关闭/删除之前,我想向您指出几件事:

This question is one of many dupes, a bad fit for SO, and will be closed. However, before it gets closed/deleted, I'd like to point a couple of things out to you:

第一件事:

当我目前在电子商务网站上工作时,我想知道在我的PDO连接上使用单例模式是否真的有用

As I'm currently working on an e-commerce website, I'd like to know if it's really useful to use a singleton pattern on my PDO connections

不,从来没有.

PHP无法在两次请求之间保留对象的状态.服务器收到请求.解析脚本,建立数据库连接,执行查询,发送回响应,并关闭连接.一遍又一遍.
连接不能被序列化,它们不能以任何方式,形状或形式存储,因此:
通过良好的编码,您可以同时使用注入,以确保每个请求仅连接一次数据库.
现在,静力学是全局变量:如果您还想传递一个参数,您会使用全局变量吗?为您带来疑问,我想您的回答是.
因此,剩下的单例剩余的一些好处不值得为使用单例的代码编写像样的,有效的单元测试之类的工作.

No, it never is.

PHP is not capable of persisting an objects' state in between requests. The server receives a request. The script is parsed, the DB connection is established, queries are performed, the response is sent back and the connection is closed. Over and over again.
Connections can't be serialized, they can't be stored in any way, shape or form, so:
By coding well, you can use injection and at the same time ensure that you only connect to the DB once per request.
Statics are globals in drag, now: would you use globals if you might as well pass an argument? Giving you the benefit of the doubt, I assume your answer is no.
The few remaining benefits of singletons that remain, then, are not worth the effort it takes to write decent, working, unit-tests for your singleton-using code.

为什么:

我的体系结构的构建方式是每个脚本只需要一个连接.

my architecture is made in a way that each script only needs one connection.

然后您的代码不是很可扩展.我知道这是一条笼统的声明,而且是陈词滥调,但事实就是如此.就像您假定每个脚本只需要一个连接的假设一样,现在 可能是正确的,但是谁能说总是这样?
重构数据库,添加对另一个适配器的支持,测试(如前所述)变得越来越困难,因为您将编写代码,并且在不注意的情况下,新代码将适于维护单例.
这意味着您将编写代码以保留原来的内容,因为您正在解决它的问题,这是一个错误的设计决策.

Then your code isn't very scalable. I know this is a blanket statement, and a terrible cliché, but it just is the case here. Just like your presumtion that each script only needs one connection might be true right now, but who's to say that will always be the case?
Refactoring the DB, adding support for another adapter, testing (as mentioned before) become increasingly difficult, as you'll write code and, without noticing it, that new code will be adapted to maintain the singleton.
This means that you'll write code to preserve what was, because you're working around its problems, a bad design decision.

这样想:
您要盖房子,然后决定在走廊上建一个混凝土桩,因为无论如何这就是您要挂衣架的地方,而且您永远都不会改变.但是Singleton意味着您正在编写OO代码.重点是您可以重用代码.现在:您是否诚实地认为,如果拥有不动的衣架,您将能够像出售东西一样轻松地出售房屋?当然不是!

Think of it like this:
You build a house, and decide to build a concrete pilar into the corridor, because that's where you'll hang your coat-hangers anyway, and you'll never change that. But Singleton implies that you're writing OO code. The whole point is that you can re-use code. Now: do you honestly think that you'll be able to sell your house just as easily if it had an immovable coat-hanger, as it would be if that thing wouldn't be there? of course not!

我非常确定,它仅在您必须两次或多次调用实例时有用.

And I'm quite sure that it's only useful when you have to call an instance twice or more.

如果是这样的话:请使用另一种模式,或者从Symfony书中摘下一片叶子,并使用容器使该连接可用于任何需要它的对象(依赖注入组件值得Google搜索).

If that's so: use another pattern, or take a leaf out of the Symfony book, and make the connection available to whatever object needs it using a container (dependency injection component is worth a google search).

测试:

所有之前已经解释过,但是测试单例代码是一场噩梦.学习使用注射.

It's all been explained before, but testing singleton code is a nightmare. Learn to use injection.

这篇关于PDO + Singleton:为什么要使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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