explain $CI =& get_instance(); explain $CI =& get_instance(); php php

explain $CI =& get_instance();


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


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.


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 workClass Car{   $this->load->library('something');   $this->load->helper('something');}//this will workClass Car{   $CI=&get_instance();   $CI->load->library('something');   $CI->load->helper('something');}// Here $CI is a variable.