Sanjay Khatri
Sanjay Khatri

Reputation: 4211

CodeIgniter Basic

I am new to codeigniter.

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

is this use for error logging or global config variable.

thanks in advance.

Upvotes: 0

Views: 245

Answers (2)

Khant Wai Kyaw
Khant Wai Kyaw

Reputation: 138

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 work
Class Car
{
   $this->load->library('something');
   $this->load->helper('something');
}


//this will work
Class Car
{
   $CI=&get_instance();
   $CI->load->library('something');
   $CI->load->helper('something');
}
// Here $CI is a variable.

Upvotes: 0

Diablo
Diablo

Reputation: 3418

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

Upvotes: 9

Related Questions