人们为什么在他们的PHP框架中使用单例 [英] Why people use singletons in their PHP framework

查看:63
本文介绍了人们为什么在他们的PHP框架中使用单例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我正在努力理解为什么需要单身人士.

Ok guys I am struggling to understand why there is a need of singleton.

让我们举一个真实的例子:我有一个用于CMS的框架
我需要一个记录一些信息的类(让我们在PHP上坚持下去).

Let's make a real example: I have a framework for a my CMS
I need to have a class that logs some information (let's stick on PHP).

示例:

class Logger{
   private $logs = array();

   public function add($log) {
      $this->logs[]=$log;
   }      
}

现在,对于我的CMS页面请求的输入寿命,该 helper 对象必须是唯一的.
为了解决这个问题,我们将其设为单例(声明构造函数为私有,等等)

Now of course this helper object must be unique for the entry life of a page request of my CMS.
And to solve this we would make it a singleton (declaring private the constructor etc.)

但是为什么在地狱里这样的类不是完全静态的?这样可以解决单例模式的需要(这被认为是不切实际的)示例:

But Why in the hell a class like that isn't entirerly static? This would solve the need of the singleton pattern (that's considered bad pratice) Example:

class Logger {
    private static $logs = array();

    public static function add($log) {
        self::$logs[]=$log;
    }
}

通过使此帮助程序完全静态,当我们需要在应用程序中的某处添加日志时,我们只需要像Logger::add('log 1');那样静态调用它,而不是像Logger::getInstance()->add('log 1');

By making this helper entirely static, when we need to add a log somewhere in our application we just need to call it statically like: Logger::add('log 1'); vs a singleton call like: Logger::getInstance()->add('log 1');

希望有人会让我很容易理解为什么在PHP中对静态类使用单例.

Hope someone makes it easy to understand for me why use singleton over static class in PHP.

这很漂亮感谢@James,对有关谁感兴趣的单例与静态课程进行了精彩的演讲. (请注意,它没有解决我的问题)

This is a pretty nice lecture on the singleton vs static class for who is interested, thanks to @James. (Note it doesn't address my question)

推荐答案

很多原因.

静态方法基本上是可以从任何范围调用的全局函数,这使其很难跟踪错误.您可能根本不使用任何类.

Static methods are basically global functions that can be called from any scope, which lends itself to hard to track bugs. You might as well not use a class at all.

由于您不能使用__construct方法,因此可能必须将init静态方法放在某个地方.现在,他们的代码中的人们不确定是否曾经调用过init方法.他们会再次打电话吗?他们是否必须在代码库中搜索此调用?如果init在某个地方但是被删除或中断怎么办?现在,代码中的许多位置都依赖于调用init方法的位置.

Since you cannot have a __construct method, you may have to put an init static method somewhere. Now people in their code are unsure if the init method has been called previously. Do they call it again? Do they have to search the codebase for this call? What if init was somewhere, but then gets removed, or breaks? Many places in your code now rely on the place that calls the init method.

众所周知,静态方法很难在许多单元测试框架中进行单元测试.

Static methods are notoriously hard to unit test with many unit testing frameworks.

还有更多原因,但是很难一一列举.

There are many more reasons, but it's hard to list them all.

如果您使用的是DI,也根本不需要单字.

Singletons aren't really needed either if you are using DI.

旁注. DI允许您的类不相互依赖,而是相互依赖.由于它们之间的关系没有得到巩固,因此以后更改应用程序会更容易,并且一个类的中断不会破坏这两个类.

A side note. DI allows for your classes not to rely on each other, but rather on interfaces. Since their relationships are not cemented, it is easier to change your application at a later time, and one class breaking will not break both classes.

在某些情况下,单个状态类是可行的,例如,如果您的方法都不依赖其他方法(基本上没有方法改变类的状态).

There are some instances where single state classes are viable, for instance if none of your methods rely on other methods (basically none of the methods change the state of the class).

这篇关于人们为什么在他们的PHP框架中使用单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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