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

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

问题描述

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



get_instance()定义在 Codeigniter .php

  //加载基本控制器类
require BASEPATH.'core / Controller.php';

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

并且 CI_Controller 定义在 Controller.php

  class CI_Controller {

private static $ instance;

/ **
*构造函数
* /
public function __construct()
{
self :: $ instance =& $ this;

//将
// bootstrap文件(CodeIgniter.php)实例化的所有类对象分配给本地类变量
//,以使CI可以作为一个运行大超级对象。
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;
}
}

以下是推荐在< a href =http://codeigniter.com/user_guide/general/creating_libraries.html>创建库的用户指南:


在您的库中使用CodeIgniter资源



要访问库中的CodeIgniter的原生资源,请使用
get_instance c $ c>函数。这个函数返回CodeIgniter超级
对象。



通常从你的控制器函数中,你将调用
任何一个可用的CodeIgniter函数,使用 $ this construct:
$ this-> load-> helper('url'); $ this-> load-> library('session');
$ this-> config-> item('base_url');
等。



$ this ,但是,只能直接在你的控制器,你的
模型或你的意见。如果你想在自己的自定义类中使用CodeIgniter的类
,你可以这样做:



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



$ CI =& get_instance();



将对象分配给变量后,您将使用
变量而不是 $ this
$ CI =& get_instance();
$ CI-> load-> helper('url'); $ CI-> load-> library('session');
$ CI-> config-> item('base_url');



注意:你会注意到上面的 get_instance()函数是
通过引用:



$ CI =& 通过引用分配允许您使用
原始的CodeIgniter对象,而不是创建一个


相关文章: explain $ CI =& get_instance(); / Codeigniter:获取实例



这里是我的实际问题:



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



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

  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');很好的例子,但我经常看到这个例子。
}
}


$ b为什么要麻烦这个方法,而不是直接调用 get_instance()? 似乎将整个Controller对象分配给类变量不是一个好主意,即使它是一个引用。也许没关系。



我想为 get_instance()写一个包装函数,类型,我不必不断地将它分配给一个变量。

  function CI()
{
return get_instance();
}

或:

 函数CI()
{
$ CI =& get_instance();
return $ CI;
}

然后我可以使用 class-> method()从任何地方都没有分配给一个变量的麻烦,它很容易写和理解它的作用,并可以导致更短,更优雅的代码。 >


  • 是否有理由不采取这种方法?

  • 两者之间有什么区别 CI()上面的函数

  • 为什么建议将 get_instance() c> function& get_instance()中的& {} 是什么意思?我知道有什么参考资料适用,我在适当时使用它们,但我从来没有见过这样定义的函数。如果我写了一个包装函数,我应该使用这个吗?



请注意,这不是一个风格问题,但是技术问题。



EDIT

/ strong>:到目前为止,我们有:




  • 方法链接在php4中不可用,所以分配给变量是一个解决方法因为Codeigniter已经删除了php4支持)

  • 调用一个函数多次返回对象而不是调用一次并分配给一个变量的次要开销。



还有什么,还是这些只是潜在的问题?

解决方案

据我所知,这是一个方便的事情比任何事情。有可能你将在库中使用CI超级对象,为什么不将它分配给一个变量使它更容易使用?



是一些其他的事情要考虑...


  1. 如果你把这个方法放在一个帮助器中,那个方法就成为任何类的依赖这可能不是一个大问题,但如果你想与任何人共享库,他们可能不满意依赖,尤其是因为已经有一个标准的方式来处理这个在CI社区。

  2. 每次使用助手时调用 get_instance(),而不是将其结果存储一个变量。

  3. 由于这是一个帮助方法,应该为您节省时间,对于那些主要在CI的核心MVC文件中工作的人来说,设置一个这样的帮助


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() is defined in Codeigniter.php

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

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

And CI_Controller is defined in 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:

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.

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

First, assign the CodeIgniter object to a variable:

$CI =& get_instance();

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.

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

$CI =& get_instance();

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

Related posts: explain $CI =& get_instance(); / Codeigniter: Get Instance

So, here is my actual question:

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?

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');
    }
}

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.

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();
}

Or:

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

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.

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

  • Method chaining is not available in php4, so assigning to a variable is a workaround (although this is fairly irrelevant as Codeigniter has dropped php4 support)
  • The minor overhead of calling a function more than once to return the object, rather than calling it once and assigning to a variable.

Anything else, or are these the only potential issues?

解决方案

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. 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天全站免登陆