Home > Article > Backend Development > php $CI =& get_instance();, phpci_PHP tutorial
See someone writing this for beginners, $CI =& get_instance();
To access the original resources of CodeIgniter in your custom class library, you must use the get_instance() function. This function returns a CodeIgniter super object.
Generally speaking, in your controller function you Any available CodeIgniter function can be called via $this:
$this->load->helper('url');
$this->load->library('session' );
$this->config->item('base_url');
etc.
$this, only directly affects your own controllers, models and views. When you When you want to use the original CodeIgniter class in a custom class, you can do this:
First, define the CodeIgniter object and assign it to a variable:
$CI =& get_instance();
Once you define an object as a variable, you can use that variable name instead of $this:
$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 by reference:
$CI =& get_instance();
This is very important. By reference Assigning to a variable will use the original CodeIgniter object 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 objects located in constructors because the object does not exist until the class is fully instantiated.