PHP 中的注册表或单例模式? [英] Registry or Singleton pattern in PHP?

查看:51
本文介绍了PHP 中的注册表或单例模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在处理 PHP 类和对象.在这个问题中,字段和方法的名称只是为了让您了解我在说什么.

I am working with PHP classes and objects now. In this question the names of fields and methods are made up just so you get an idea of what I am talking about.

它与使用单例和注册表设计模式有关.

It is related to using the singleton and registry design patterns.

现在假设我需要访问几乎所有其他类中的数据库对象、缓存对象、核心设置对象、会话对象,我需要访问这些对象.所以我会使用注册表将所有 4 个对象存储到 1 个注册表类对象中.然后我可以轻松地将我的 1 个对象传递给需要访问这些对象的任何其他对象.到目前为止,这听起来不错,但是如果我有一些不需要所有 4 个对象的类,如果我只需要访问其他一些类中的 Database 对象或 Session 对象怎么办?为了性能,最好在这些其他对象中使用单例,还是继续在这些对象中使​​用我的注册表?

Now lets say I need to access a Database object, Cache Object, Core Settings object, Session object in almost every other class I will need to have access to these. SO I would use a registry to store all 4 of those object into 1 registry class object. I could then easiyl just pass in my 1 object into any other object that needs to access these. So that sounds great so far but what if I have some classes that do not need all 4 of those objects, what If I ONLY need to access the Database object or the Session object in some of my other classes? For perfromance would it be best to just use a singleton inside these other objects or would it be the same to go ahead and use my registry in these?

我不太了解对象在 PHP 中的工作方式,无法知道是否会有任何性能提升(更少的内存使用、CPU 使用、加载时间).

I do not know well enough how the objects work in PHP to know if there would be any sort of performnce gain (less memory usage, cpu usage, load time).

所以任何有这方面经验的人都可以告诉我使用其中一种是否会有任何收益,我正处于可以在不影响生产时间或任何事情的情况下进行以太的阶段.如果可以的话,我想用最好的方法.

So anyone with experience in this maybe can tell me if there would be any gain using one or the other, I am at the stage where I can go ether way without it affecting production time or anything. I would like to use the best method if I can now.

推荐答案

这取决于您的应用程序.如果您仍然需要 4 个类中的 3 个,那么使用注册表比单独处理 3 个更理想,因为您不需要第四个.延迟加载类将是减少内存占用的一种方法,但是您需要指示注册表何时创建对象,这与处理单例没有太大区别.或者,您可以创建一个 n 参数构造函数或使用一个数组来指示您的注册表在构造过程中实例化哪些类.

That depends on your application. If you still need 3 out of the 4 classes, then it'd be more ideal to use the Registry than to handle the 3 independently only because you don't need the fourth. Loading the classes lazily would be one approach to reduce memory footprint, but then you need to instruct the registry when to create the objects and that's not much different than handling singletons. Alternatively, you could create an n-parameter constructor or use an array to instruct your Registry which classes to instantiate during construction.

class Registry {
    public $class1;
    public $class2;

    function __construct($uses) {
        foreach($uses as $class) {
            $this->{$class} = new {$class}();
        }
    }

}

然后通过指定要实例化的类来实例化您的注册表.

Then instantiate your Registry by specifying which classes to instantiate.

$reg = new Registry(array('class1'));

您显然希望您的构造函数处理零参数以说明默认情况下实例化所有类.

You would obviously want your constructor to handle zero parameters to account for instantiating all classes by default.

这篇关于PHP 中的注册表或单例模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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