关于PHP单子类的最佳实践[英] Best practice on PHP singleton classes

本文是小编为大家收集整理的关于关于PHP单子类的最佳实践的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

可能的重复:
谁需要单身人士?

我总是写关于最佳实践的写作,但我也想了解为什么给定的事物是最佳实践.

我在文章(不幸的是我不记得)中读到的是,辛格尔顿类是可以实例化的,而不是用静态功能制成,并使用范围分辨率运算符(::)访问.因此,如果我有一个包含我所有可以验证的工具的课程,那么简而言之:

class validate {
    private function __construct(){}
    public static function email($input){
        return true;
    }
}

我被告知这被认为是不良练习(或至少要警告),因为垃圾收集器和维护.因此,"单身班作为静态方法"的批评是,我实例化了一个类,我100%确定我只会实例化一次.对我来说,这似乎是在做"双重工作",因为那里都准备好了.我想念什么?

对此事有何看法?当然,这不是生与死问题,但如果有选项,则可能会以正确的方式做一个事情:)

推荐答案

php中的单例课程示例:
在PHP5中创建Singleton设计模式:ANS 1:
在php5中创建单例设计模式:ans 2:

Singleton is considered "bad practice".

主要是因为:在PHP中如何测试注册表模式或单身人士?

想阅读更多? :

单人决策图( source )/p>

 Singleton决策图

其他推荐答案

单例对象是仅实例化一次的对象.这与 singem> singleton pattern 抗)模式如何编写 can 仅实例化的类, singleton (开始时大 s ):

"确保课堂只有一个实例,并提供了全球访问点."

就PHP而言,通常您不需要实现单例模式.实际上,当您要求最佳实践时,您应该避免这样做,因为它是不良练习.

此外,您发现的大多数PHP代码示例是忽略PHP工作方式的模式的半就已经准备就绪的实现.这些虚假的实现不符合模式中的"确保".

这也告诉一些事情:通常不需要它.如果草率的实现已经完成了工作,而甚至没有接近模式的目的,则使用了错误的模式,它已经开始成为 anti> anti>.

在PHP中,通常无需确保一类只有一个实例,PHP应用程序并不是您需要的那么复杂(例如,没有多个线程可能需要参考原子实例).

通常剩下的是类实例的全局访问点,它适用于大多数PHP开发人员(MIS-)使用该模式.众所周知,使用这种"单例"导致了全球静态状态的标准问题,这些问题将复杂性跨多个级别引入您的代码中并降低可重复性.作为程序员,您会失去以灵活的方式使用代码的能力.但是弹性是解决问题的非常重要的技术.程序员整天都在解决问题.

因此,在应用A 设计模式之前,需要评估Pro和Cons.只是最常使用某种模式没有帮助.

对于初学者,我会说,只需写您的课程,请注意如何以及何时在应用程序逻辑的其他部分进行实例化,以便保持灵活性.

其他推荐答案

好吧,这实际上不是单身人士;单身人士确保您只有一个类的实例,并且这里没有方法可以检索validate的单个实例.您在这里的设计似乎是静态课程.这不会引起垃圾收集器的问题(至少您在此处放置的代码),因为无论如何都将加载到内存中.

本文地址:https://www.itbaoku.cn/post/627473.html

问题描述

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?

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 :)

推荐答案

An example singleton classes in php:
Creating the Singleton design pattern in PHP5 : Ans 1 :
Creating the Singleton design pattern in PHP5 : Ans 2 :

Singleton is considered "bad practice".

Mainly because of this: How is testing the registry pattern or singleton hard in PHP?

Wanna read more? :

A Singleton decision diagram (source):

Singleton Decision Diagram

其他推荐答案

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 can be only instantiated once, the Singleton (large S at the beginning):

“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.

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.

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).

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.

So before applying a Design Pattern the 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.

其他推荐答案

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.