Codeigniter 中的 get_instance():为什么将其分配给变量? [英] get_instance() in Codeigniter: Why assign it to a variable?

查看:33
本文介绍了Codeigniter 中的 get_instance():为什么将其分配给变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Codeigniter 中,get_instance() 是一个全局可用的函数,它返回包含所有当前加载类的 Controller 超级对象(它返回 Controller 类实例).我将包括当前的源代码:

In Codeigniter, get_instance() is a globally available function that returns the Controller super-object which contains all the currently loaded classes (it returns the Controller class instance). I'll include the current source code:

get_instance() 定义在 Codeigniter.php

// Load the base controller class
require BASEPATH.'core/Controller.php';

function &get_instance()
{
    return CI_Controller::get_instance();
}

CI_Controller定义在Controller.php

class CI_Controller {

    private static $instance;

    /**
     * Constructor
     */
    public function __construct()
    {
        self::$instance =& $this;

        // Assign all the class objects that were instantiated by the
        // bootstrap file (CodeIgniter.php) to local class variables
        // so that CI can run as one big super object.
        foreach (is_loaded() as $var => $class)
        {
            $this->$var =& load_class($class);
        }

        $this->load =& load_class('Loader', 'core');

        $this->load->set_base_classes()->ci_autoloader();

        log_message('debug', "Controller Class Initialized");
    }

    public static function &get_instance()
    {
        return self::$instance;
    }
}

以下是用户指南以创建库的推荐使用方法:

Here's how it is recommended to be used in the user guide for creating libraries:

要在您的库中访问 CodeIgniter 的本机资源,请使用get_instance() 函数.此函数返回 CodeIgniter super对象.

Utilizing CodeIgniter Resources within Your Library

To access CodeIgniter's native resources within your library use the get_instance() function. This function returns the CodeIgniter super object.

通常在你的控制器函数中你会调用任何使用 $this 结构的可用 CodeIgniter 函数:$this->load->helper('url');$this->load->library('session');$this->config->item('base_url');

Normally from within your controller functions you will call any of the available CodeIgniter functions using the $this construct: $this->load->helper('url'); $this->load->library('session'); $this->config->item('base_url'); etc.

$this 只能直接在您的控制器中工作,您的模型,或您的意见.如果你想使用 CodeIgniter 的类在您自己的自定义类中,您可以执行以下操作:

$this, however, only works directly within your controllers, your models, or your views. If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:

首先,将 CodeIgniter 对象赋值给一个变量:

First, assign the CodeIgniter object to a variable:

$CI =&get_instance();

$CI =& get_instance();

一旦您将对象分配给变量,您将使用它变量而不是 $this:$CI =&获取实例();$CI->load->helper('url');$CI->load->library('session');$CI->config->item('base_url');等

Once you've assigned the object to a variable, you'll use that variable instead of $this: $CI =& get_instance(); $CI->load->helper('url'); $CI->load->library('session'); $CI->config->item('base_url'); etc.

注意:您会注意到上面的 get_instance() 函数正在通过引用传递:

Note: You'll notice that the above get_instance() function is being passed by reference:

$CI =&get_instance();

$CI =& get_instance();

这很重要.通过引用分配允许您使用原始 CodeIgniter 对象,而不是创建它的副本.

This is very important. Assigning by reference allows you to use the original CodeIgniter object rather than creating a copy of it.

相关帖子:解释 $CI =&get_instance();/Codeigniter:获取实例

所以,这是我的实际问题:

So, here is my actual question:

为什么用户指南建议将 get_instance() 分配给变量?我相当肯定我理解不通过引用分配的含义,但是为什么建议在 get_instance()->load->model() 工作正常时将其分配给变量?

Why does the user guide recommend assigning get_instance() to a variable? I'm fairly certain I understand the implications of not assigning by reference, but why is it recommended to assign it to a variable when get_instance()->load->model() works fine?

我在 CI 中看到很多用户定义或第三方类分配给对象的属性:

I see a lot of user defined or third party classes in CI that assign to a property of the object:

class MY_Class {

    private $CI;

    function __construct()
    {
        $this->CI =& get_instance();
    }
    function my_func()
    {
        $this->CI->load->view('some_view');
    }
    function my_other_func()
    {
        $this->CI->load->model('some_model');
    }
}

不好的例子,但我经常看到这个.为什么要使用这个方法而不是直接调用 get_instance() 呢?似乎将整个 Controller 对象分配给一个类变量并不是一个好主意,即使它是一个引用.也许没关系.

Poor example, but I see this frequently. Why bother with this method instead of just calling get_instance() directly? It seems like assigning the entire Controller object to a class variable wouldn't be a great idea, even if it is a reference. Maybe it doesn't matter.

我想为 get_instance() 编写一个包装函数,这样输入起来更容易,而且我不必经常将它分配给变量.

I want to write a wrapper function for get_instance() so it's easier to type, and I don't have to constantly assign it to a variable.

function CI()
{
    return get_instance();
}

或者:

function CI()
{
    $CI =& get_instance();
    return $CI;
}

然后我可以在任何地方使用 CI()->class->method() 而无需将其分配给变量的麻烦,很容易编写和理解它的作用,并且可以产生更短、更优雅的代码.

Then I could use CI()->class->method() from anywhere without the hassle of assigning it to a variable, it's very easy to write and understand what it does, and can result in shorter, more elegant code.

  • 是否有任何理由不采用这种方法?
  • 上面的两个 CI() 函数有什么区别吗?
  • 为什么建议将 get_instance() 分配给变量而不是直接调用它?
  • function &get_instance(){} 中的 & 是什么意思?我对 references 的用途有所了解,我会在适当的时候使用它们,但我从未见过以这种方式定义的函数.如果我确实编写了一个包装函数,我是否也应该使用它?
  • Is there any reason not to take this approach?
  • Is there any difference between the two CI() functions above?
  • Why is it recommended to assign get_instance() to a variable rather than calling it directly?
  • What does the & in function &get_instance(){} mean where it is defined? I know a bit about what references are for and I use them when appropriate, but I've never seen a function defined this way. If I do write a wrapper function, should I use this as well?

请注意,这与其说是一个风格问题,不如说是一个技术问题.我想知道使用我建议的方法是否存在任何问题,性能或其他.

Please note that this is not so much a style question, but a technical one. I want to know if there are any issues, performance or otherwise, with using the method I'm suggesting.

编辑:到目前为止,我们有:

EDIT: So far we have:

  • 方法链在 php4 中不可用,因此分配给变量是一种解决方法(尽管这与 Codeigniter 已放弃对 php4 的支持无关)
  • 多次调用函数以返回对象的小开销,而不是调用一次并分配给变量.

还有其他问题吗,或者这些是唯一的潜在问题吗?

Anything else, or are these the only potential issues?

推荐答案

据我所知,这比什么都重要.您可能会在库中大量使用 CI 超级对象,那么为什么不将其分配给一个变量以使其更易于使用呢?

As far as I know, it's a matter of convenience more than anything. Chances are that you will be using the CI super object a lot in your libraries so why not assign it to a variable to make it a little easier to work with?

还有一些其他的事情需要考虑......

There are a few other things to consider...

  1. 如果您将此方法放在帮助程序中,该方法将成为您使用它的任何类的依赖项.这对您来说可能不是什么大问题,但如果您想与其他人共享库,他们可能不会对依赖关系感到高兴,尤其是因为 CI 社区已经有一种标准的方法来处理这个问题.
  2. 对性能有轻微影响,因为您每次使用帮助程序时都会调用 get_instance(),而不是将其结果存储在变量中.
  3. 由于这是一个可以节省您时间的辅助方法,对于主要在 CI 的核心 MVC 文件中工作的任何人来说,设置这样的辅助方法比仅将其设置为少数几个变量需要更长的时间需要的地方.
  1. If you put this method in a helper, that method becomes a dependency for any class you are using it in. This might not be a big deal for you, but if you want to share libraries with anyone else they may not be happy about the dependency, especially since there is already a standard way of handling this in the CI community.
  2. There is a slight impact on performance because you are calling get_instance() every time you use the helper rather than storing its result in a variable.
  3. Since this is a helper method that is supposed to save you time, for anyone who is working mostly in the core MVC files of CI, setting up a helper like this would take longer than just setting it to a variable in the few places you need it.

这篇关于Codeigniter 中的 get_instance():为什么将其分配给变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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