解释$ CI =&get_instance();

查看codeigniter的源代码,

在其帮助函数中,我$CI =& get_instance();看到代码$CI =& get_instance(); 任何人都可以向我解释这个代码是如何工作的?

我得到它是返回一个引用到$超级对象,但get_instance()从哪里来?

它基本上是一个使用函数而不是静态方法的Singletondevise模式 。

要深入了解,请查看源代码

所以基本上,它不强制单身人士,但它是一个公共职能的捷径…

编辑:其实,现在我明白了。 为了兼容PHP4,他们必须做一个双全局variables的破解 ,才能正确地返回引用。 否则,引用将全部搞砸。 而且由于PHP4不支持静态方法(不pipe怎么说),使用函数是更好的方法。 所以它仍然存在,因为传统的原因…

因此,如果你的应用程序只是PHP5,那么做CI_Base::get_instance(); 应该没有问题CI_Base::get_instance(); 相反,它是相同的…

在某些情况下,您可能希望开发远离控制器的类,但可以利用所有的Code Igniter资源。 这很容易通过使用get_instance()函数。

在控制器函数中实例化的任何类都可以使用get_instance()函数访问Code Igniter的本地资源。 这个函数返回主要的Code Igniter对象。

通常,调用任何可用的代码点火器function都需要使用$ this构造:

 $this->load->helper('url'); $this->load->library('session'); $this->config->item('base_url'); 

等等

然而, $ this仅适用于您的控制器,模型或视图。 如果您想在自定义类中使用Code Igniter的类,可以这样做:

首先,将Code Igniter对象分配给一个variables:

 $obj =& get_instance(); 

一旦你将对象赋值给一个variables,你将使用该variables而不是$ this

 $obj =& get_instance(); $obj->load->helper('url'); $obj->load->library('session'); $obj->config->item('base_url'); 

等等

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

 $obj =& get_instance(); 

这个非常重要。 通过引用分配允许您使用原始代码点火器对象,而不是创build它的副本。

你可以从这里阅读关于这个function

get_instance()是CodeIgniter核心文件中定义的一个函数。 当您处于超级对象之外的作用域中时,您可以使用它来获取对CodeIgniter超级对象的单例引用。

我很确定它是在base.php或类似的东西中定义的。

这是一个单独的结构来理解codeigniter如何加载库和类

 <?php /* ==================================== start of the loader class ==================================== */ class Loader { protected function _init_class($class){ $C = Controller::get_instance(); $name = strtolower($class); $C->$name = new $class(); } public function _class($library){ if(is_array($library)){ foreach($library as $class){ $this->library($class); } return; } if($library == ''){ return false; } $this->_init_class($library); } public function view ($param) { echo $param; } } /* =============================== End of the loader class ============================== */ /* =============================== start of core controller class ============================== */ class Controller { private static $instance; function __construct () { self::$instance = $this; $this->load = new Loader(); } public static function get_instance(){ return self::$instance; } } /* =============================== end of the core controller class =================================== */ /* ==================================================== start of library sections (put all your library classes in this section) ===================================================== */ class MyLibrary { private $c; function __construct() { $this->c = Controller::get_instance(); } function say($sentence) { $this->c->load->view($sentence); } } /* ==================================================== End of the library sections ==================================================== */ /* ============================================ start of controller section (put all your controller classes in this section) =========================================== */ class Foo extends Controller { function __construct () { parent::__construct(); $this->load->_class('MyLibrary'); } function bar() { $this->mylibrary->say('Hello World'); } } /* ========================================== End of the controller sections ========================================== */ $foo = new Foo(); $foo->bar(); 

只有扩展了CI_Controller,Model,View的类才能使用

 $this->load->library('something'); $this->load->helper('something');//..etc 

您的自定义类不能使用上面的代码。 要在自定义类中使用上述function,您必须使用

 $CI=&get instance(); $CI->load->library('something'); $CI->load->helper('something'); 

例如,在您的自定义类中

 // this following code will not work Class Car { $this->load->library('something'); $this->load->helper('something'); } //this will work Class Car { $CI=&get_instance(); $CI->load->library('something'); $CI->load->helper('something'); } // Here $CI is a variable.