CodeIgniter get_instance(), when to use it?
get_instance() is a function defined in the core files of CodeIgniter. Is use it to get the singleton reference to the CodeIgniter super object when you are in a scope outside of the super object.
In a controller, a model or a view that is under CodeIgniter core files you do not need to use get_instance.
For example if you want to load a library or helper use
$this->load->library('...'); $this->load->helper('...');
But if you want to load a library or helper and use the code above in a class which is out outside CodeIgniter core files, you will get this error: “Using $this when not in object context …”
To get access to the Codeigniter functions You should use get_instance() function, like:
$CI = &get_instance(); $CI->load->library('something'); $CI->load->helper('something');
Be careful! You have to use assignment by reference, because the object returned by get_instance exist only once, so we can’t duplicate it or clone it.