...
CodeIgniter, Tutorials

How to Creating Ancillary Classes in Codeigniter?

12 March 2019

In some cases we want to develop Ancillary Classes that exist apart from our controllers but have the ability to utilize all of CodeIgniter’s resources. This is easily possible as we’ll see.

get_instance()

Normally, to call any of the available CodeIgniter functions requires to use the $this construct:

For Example:

$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');

$this, however, only works within our controllers, our models, or our views. If you would like to use CodeIgniter’s classes from within our own custom classes we can do so as follows:

First, assign the CodeIgniter object to a variable:

$CI =& get_instance();

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

For Example:

$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');

Leave a Comments