简体   繁体   English

PHP中的单例和类实例化

[英]Singleton and class instantiation in php

There is a class like this in codeigniter framework ( I edited it to be more clear, full function is here http://pastebin.com/K33amh7r ): 在codeigniter框架中有一个类似这样的类(为了更清晰起见,我对其进行了编辑,完整的功能在此处http://pastebin.com/K33amh7r ):

function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
    {
        static $_classes = array();

        // Does the class exist?  If so, we're done...
        if (isset($_classes[$class]))
        {
            return $_classes[$class];
        }


        is_loaded($class);

        $_classes[$class] = new $name();
        return $_classes[$class];

    }

So, first time when class is loaded ( passed to this function), it will be saved to this static variable. 因此,第一次加载类(传递给此函数)时,它将被保存到此静态变量中。 Next time when the same class is loaded, this function checks if class exists already ( if it's already assigned to static, cached, I'm not sure how in memory is this stored) and if it exists, it's loaded ( NOT * instantiated * again ) 下次加载相同的类时,此函数将检查类是否已经存在(如果已经将其分配给静态的,已缓存的,我不确定该在内存中的存储方式),如果存在,则将其加载( 不是 * 实例化 *再次)

As far as I can see, the only purpose is to save time or memory and not instantiate the same class twice. 据我所知, 唯一的目的是节省时间或内存,而不是两次实例化同一类。

My question here is: Does really instantiating a class can take up memory or consume loading time so it has to be cached like this? 我在这里的问题是:真正实例化一个类是否会占用内存或消耗加载时间,因此必须像这样进行缓存?

CodeIgniter is is geared for rapid prototyping, and is really not a good example of enterprise patterns in almost any cases. CodeIgniter专为快速原型开发而设计,在几乎所有情况下,它实际上都不是企业模式的一个很好的例子。 This behavior is related to their design choice of the relationship the "controller" has to almost all other objects; 这种行为与他们对“控制器”与几乎所有其他对象的关系的设计选择有关。 namely that there is exactly one of almost anything (only one instance of controller, only one instance of each library, etc). 即几乎所有事物中只有一个(仅控制器的一个实例,每个库的仅一个实例,等等)。 This design choice is more for rapid development (justified by the developer "not having to keep track of as much" or some such...). 这种设计选择更适合快速开发(由开发人员“不必跟踪那么多”或类似的东西来证明)。

Basically, there is memory saved by not instantiating an object (as much memory as it takes to store the object's instance variables) and if the object's constructor tries to do a fair bit of work, you can save time, too. 基本上,通过不实例化对象可以节省内存(与存储对象的实例变量一样多的内存),并且如果对象的构造函数尝试进行大量工作,您也可以节省时间。

However, the appropriateness of the single-instance imperative is clearly not universal; 但是,单实例命令的适当性显然并不普遍。 sometimes you really do want a new instance. 有时您确实需要一个新实例。 When you can justify this, choose a better framework. 当可以证明这一点时,选择一个更好的框架。

The resources and time used in instantiating a class are usually negligible. 实例化类所使用的资源和时间通常可以忽略不计。 The main reason I usually see singleton classes used is to maintain data integrity. 我通常看到使用单例类的主要原因是为了维护数据完整性。 For example, if you have a class that represents data in a database, creating multiple objects for it can cause the data to become out of sync. 例如,如果您有一个表示数据库中数据的类,则为其创建多个对象可能会导致数据不同步。 If one object changes and commits data to the DB, the other objects could have old data. 如果一个对象更改并将数据提交到DB,则其他对象可能具有旧数据。

Its rather a simple concept, utilizing singleton-pattern it makes sure that one class is instantiated only once during an application's execution cycle. 它的概念很简单,利用单例模式可以确保在应用程序的执行周期中仅一次实例化一个类。

This sort of concept apply for libraries more. 这种概念更适用于图书馆。 Lets see a basic example: 让我们看一个基本的例子:

class Authenticate {
     public function login($username, $password) {
        ....
     }

     public function logout() {

     }
}

Now, through a execution of a page, there is hardly any case that the object of the above class, needs to be created more than once. 现在,通过执行页面,几乎不存在需要多次创建上述类的对象的情况。 The main thing to understand is Utilization of resources 要了解的主要是资源的利用

And YES , instantiating same class over and over again will without a doubt add up in the memory, although it might be negligible like in the example I have shown, but it does affect. 是的 ,一遍又一遍地实例化相同的类无疑会在内存中加起来,尽管它可以像我展示的示例那样忽略不计,但确实会产生影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM