Beginner PHP To see someone write this, $CI =& get_instance ();
To access CodeIgniter's original resources in your custom class library, you must use the Get_instance () function. This function returns a CodeIgniter super object.
In general, you can call any of the available CodeIgniter functions in your controller function by $this:
$this->load->helper (' url ');
$this->load->library (' Session ');
$this->config->item (' Base_url ');
etc.
$this, it only works directly in your own controllers, models, and views. When you want to use the CodeIgniter primitive class in your custom class, you can do this:
First, define the CodeIgniter object to assign to a variable:
$CI =& get_instance ();
Once you define an object as a variable, you can replace the $this with that variable name:
$CI =& get_instance ();
$CI->load->helper (' url ');
$CI->load->library (' Session ');
$CI->config->item (' Base_url ');
etc.
Note: You will notice that the get_instance () function is passed in a referenced manner:
$CI =& get_instance ();
This is very important. Assigning a variable by reference causes the original CodeIgniter object to be used, instead of creating a copy
Also, please note: If you use PHP 4, it is best not to call Get_instance () in the constructor of the class. PHP4 has a problem referencing CI Super object in the constructor because the object exists only after the class is fully instantiated.
PHP $CI =& get_instance ();