phpcodeigniterinstance

What is the purpose of CodeIgniter's get_instance() function call?


I want to know use of $CI =& get_instance();

Is this use for error logging or global config variable?


Solution

  • From CodeIgniter manual:

    $CI =& get_instance();

    Once you've assigned the object to a variable, you'll use that variable instead of $this:

    $CI =&get_instance();
    

    $CI->load->helper('url');
    $CI->load->library('session');
    $CI->config->item('base_url'); etc.
    

    Note: You'll notice that the above get_instance() function is being passed by reference:

    $CI =& get_instance();
    

    This is very important. Assigning by reference allows you to use the original CodeIgniter object rather than creating a copy of it.

    Also, please note: If you are running PHP 4 it's usually best to avoid calling get_instance() from within your class constructors. PHP 4 has trouble referencing the CI super object within application constructors since objects do not exist until the class is fully instantiated.

    Link: http://codeigniter.com/user_guide/general/creating_libraries.html