PHP 单例类的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8776788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best practice on PHP singleton classes
提问by Mark
Possible Duplicate:
Who needs singletons?
可能的重复:
谁需要单身人士?
I always write with respect to best practice, but I also want to understand why a given thing is a best practice.
我总是根据最佳实践来写作,但我也想了解为什么给定的事情是最佳实践。
I've read on in an article (I unfortunately don't remember) that singleton classes are prefered to be instantiated, rather than being made with static functions and accessed with the scope resolution operator (::). So if I have a class that contains all my tools to validate, in short:
我在一篇文章中读到(不幸的是我不记得了)单例类更喜欢被实例化,而不是使用静态函数创建并使用范围解析运算符 (::) 访问。因此,如果我有一个包含我所有验证工具的类,简而言之:
class validate {
private function __construct(){}
public static function email($input){
return true;
}
}
I've been told this is considered bad practice(or at least warned against), because of such things as the garbage collector and maintenance. So what the critiques of the "singleton class as static methods" wants, is that I instantiate a class I'm 100% certain I will only ever instantiate once. To me it seems like doing "double work", because it's all ready there. What am I missing?
我被告知这被认为是不好的做法(或至少警告过),因为诸如垃圾收集器和维护之类的事情。因此,对“作为静态方法的单例类”的批评想要的是,我实例化一个类,我 100% 确定我只会实例化一次。对我来说,这似乎是在做“双重工作”,因为一切都准备好了。我错过了什么?
What's the view on the matter? Of course it's not a life and death issue, but one might as well do a thing the right way, if the option is there :)
对此事有何看法?当然,这不是生死攸关的问题,但如果有选择的话,不妨以正确的方式做一件事:)
回答by ThinkingMonkey
An example singleton classes in php:
Creating the Singleton design pattern in PHP5 : Ans 1 :
Creating the Singleton design pattern in PHP5 : Ans 2 :
php 中的一个单例类示例:
在 PHP5 中创建单例设计模式:Ans 1:在 PHP5 中
创建单例设计模式:Ans 2:
Singleton is considered "bad practice".
Singleton is considered "bad practice".
Mainly because of this: How is testing the registry pattern or singleton hard in PHP?
主要是因为:如何在 PHP 中测试注册表模式或单例?
Wanna read more? :
想阅读更多?:
A Singleton decision diagram (source):
单例决策图(来源):
回答by hakre
A singleton object is an object that is only instantiated once. That is not the same as the Singleton Pattern, which is a (Anti-) Pattern how to write a class that canbe only instantiated once, the Singleton(large Sat the beginning):
单例对象是仅实例化一次的对象。这是不一样的Singleton模式,这是一个(反)模式如何写一个类可以只实例化一次,辛格尔顿(大小号开头):
“Ensure a class has only one instance, and provide a global point of access to it.”
“确保一个类只有一个实例,并提供一个全局访问点。”
As far as PHP is concerned, you normally do not need to implement the Singleton Pattern. In fact you should avoid to do that when you ask for best practice, because it is bad practice.
就 PHP 而言,您通常不需要实现单例模式。事实上,当您要求最佳实践时,您应该避免这样做,因为这是不好的实践。
Additionally most PHP code examples you find are half-ready implementations of the pattern that neglect how PHP works. These bogus implementations do not go conform with the "ensure" in the pattern.
此外,您发现的大多数 PHP 代码示例都是该模式的半成品实现,忽略了 PHP 的工作原理。这些虚假的实现不符合模式中的“确保”。
This also tells something: Often that it is not needed. If a sloppy implementation does the work already while not even coming close to what the pattern is for, the wrong pattern has been used for the situation, it's starting to become an Anti-Pattern.
这也说明了一些事情:通常不需要它。如果一个草率的实现已经完成了工作,而甚至没有接近模式的用途,那么错误的模式已经被用于这种情况,它开始成为反模式。
In PHP there normally is no need to ensure at all costs that a class has only one instance, PHP applications are not that complex that you would need that (e.g. there are no multiple threads which might need to refer to an atomic instance).
在 PHP 中,通常不需要不惜一切代价确保一个类只有一个实例,PHP 应用程序没有您需要的那么复杂(例如,没有可能需要引用原子实例的多个线程)。
What is often left is the global access point to the class instance, which is for what most PHP developers (mis-) use the pattern. As it's known as-of today, using such "Singletons" lead to the standard problems of global static state that introduce complexity into your code across multiple levels and decrease re-usability. As a programmer you loose the ability to use your code in a flexible way. But flexbility is a very important technique to solve problems. And programmers are solving problems the whole day long.
通常剩下的是类实例的全局访问点,这是大多数 PHP 开发人员(错误)使用的模式。正如今天所知,使用这种“单例”会导致全局静态状态的标准问题,这些问题将复杂性引入多个级别的代码并降低可重用性。作为程序员,您失去了以灵活方式使用代码的能力。但是灵活性是解决问题的一项非常重要的技术。程序员整天都在解决问题。
So before applying a Design Patternthe pro and cons need to be evaluated. Just using some pattern most often is not helpful.
因此,在应用设计模式之前,需要评估利弊。只是经常使用一些模式是没有帮助的。
For starters I would say, just write your classes and take care how and when they are instantiated in some other part of your application logic so things are kept flexible.
对于初学者,我会说,只需编写您的类并注意它们在应用程序逻辑的其他部分实例化的方式和时间,以便保持灵活。
回答by Steve Rukuts
Well, this isn't actually a singleton; a singleton ensures that you only have a single instance of a class, and there is no method here that would retrieve a single instance of Validate. Your design here appears to be a static class. This will not cause an issue with the garbage collector (at least the code you've placed here), because this would be loaded into memory no matter what.
嗯,这实际上不是一个单身人士;单例确保您只有一个类的单个实例,并且这里没有方法可以检索 Validate 的单个实例。您在这里的设计似乎是一个静态类。这不会导致垃圾收集器出现问题(至少是您放置在这里的代码),因为无论如何它都会被加载到内存中。