blob: 29f176004c1be5818bba7a1522f32ed1902c889a [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##########################
2Creating Ancillary Classes
3##########################
4
5In some cases you may want to develop classes that exist apart from your
6controllers but have the ability to utilize all of CodeIgniter's
7resources. This is easily possible as you'll see.
8
9get_instance()
10===============
11
12**Any class that you instantiate within your controller functions can
13access CodeIgniter's native resources** simply by using the
14get_instance() function. This function returns the main CodeIgniter
15object.
16
17Normally, to call any of the available CodeIgniter functions requires
18you to use the $this construct::
19
20 $this->load->helper('url'); $this->load->library('session'); $this->config->item('base_url'); etc.
21
22$this, however, only works within your controllers, your models, or your
23views. If you would like to use CodeIgniter's classes from within your
24own custom classes you can do so as follows:
25
26First, assign the CodeIgniter object to a variable::
27
28 $CI =& get_instance();
29
30Once you've assigned the object to a variable, you'll use that variable
31*instead* of $this::
32
33 $CI =& get_instance(); $CI->load->helper('url'); $CI->load->library('session'); $CI->config->item('base_url'); etc.
34
35.. note:: You'll notice that the above get_instance() function is being
36 passed by reference::
37
38 $CI =& get_instance();
39
40 This is very important. Assigning by reference allows you to use the
41 original CodeIgniter object rather than creating a copy of it.