解释 $CI = & get_instance() ;

查看解码器的源代码,

在它的 helper 函数中,我一直看到代码 $CI =& get_instance(); 有人能给我解释一下这个代码是怎么工作的吗?

我知道它返回了一个对 $CI 超级对象的引用,但是 get_instance()从哪里来呢?

97502 次浏览

get_instance() is a function defined in the core files of CodeIgniter. You use it to get the singleton reference to the CodeIgniter super object when you are in a scope outside of the super object.

I'm pretty sure it's defined in base.php or something similar.

It's basically a Singleton Design Pattern that uses a function instead of a static method.

To look deeper, check out the source code

So basically, it doesn't enforce the singleton, but it's a shortcut to a public function...

Edit: Actually, now I understand. For PHP4 compatibility they had to do a double-global-variable-hack to get it to return the references properly. Otherwise the references would get all screwed up. And since PHP4 didn't have support for static methods (well, properly anyway), using the function was the better way. So it still exists for legacy reasons...

So if your app is PHP5 only, there should be nothing wrong with doing CI_Base::get_instance(); instead, it's identical...

this is a singleton structure to understand how the codeigniter loads the libraries and classes

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

Only the class that extends CI_Controller,Model,View can use

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

Your Custom Class cannot use the above code. To use the above features in your custom class, your must use

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

for example,in your custom class

// 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.

$CI = get_instance(); is to replace $this to $CI on helper,

Putting it in the constructor worked for me:

function __construct()
{
$this->CI =& get_instance();
}