blob: 6a64742ceff5bb57a4d5367af381ba3d25ef164d [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()
Andrey Andreev16a704c2012-11-09 17:25:00 +020010==============
Derek Jones8ede1a22011-10-05 13:34:52 -050011
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020012.. php:function:: get_instance()
Andrey Andreev16a704c2012-11-09 17:25:00 +020013
14 :returns: object of class CI_Controller
15
16**Any class that you instantiate within your controller methods can
Derek Jones8ede1a22011-10-05 13:34:52 -050017access CodeIgniter's native resources** simply by using the
Andrey Andreev16a704c2012-11-09 17:25:00 +020018``get_instance()`` function. This function returns the main
19CodeIgniter object.
Derek Jones8ede1a22011-10-05 13:34:52 -050020
Andrey Andreev16a704c2012-11-09 17:25:00 +020021Normally, to call any of the available CodeIgniter methods requires
22you to use the ``$this`` construct::
Derek Jones8ede1a22011-10-05 13:34:52 -050023
Derek Jonesaf8da302011-10-05 17:40:07 -050024 $this->load->helper('url');
25 $this->load->library('session');
26 $this->config->item('base_url');
27 // etc.
Derek Jones8ede1a22011-10-05 13:34:52 -050028
Andrey Andreev16a704c2012-11-09 17:25:00 +020029``$this``, however, only works within your controllers, your models,
30or your views. If you would like to use CodeIgniter's classes from
31within your own custom classes you can do so as follows:
Derek Jones8ede1a22011-10-05 13:34:52 -050032
33First, assign the CodeIgniter object to a variable::
34
35 $CI =& get_instance();
36
37Once you've assigned the object to a variable, you'll use that variable
Andrey Andreev16a704c2012-11-09 17:25:00 +020038*instead* of ``$this``::
Derek Jones8ede1a22011-10-05 13:34:52 -050039
Derek Jonesaf8da302011-10-05 17:40:07 -050040 $CI =& get_instance();
41
42 $CI->load->helper('url');
43 $CI->load->library('session');
44 $CI->config->item('base_url');
45 // etc.
Derek Jones8ede1a22011-10-05 13:34:52 -050046
Andrey Andreev16a704c2012-11-09 17:25:00 +020047.. note:: You'll notice that the above get_instance() ``function`` is being
Derek Jones8ede1a22011-10-05 13:34:52 -050048 passed by reference::
49
Derek Jonesaf8da302011-10-05 17:40:07 -050050 $CI =& get_instance();
Derek Jonesb8c283a2013-07-19 16:02:53 -070051
Derek Jones8ede1a22011-10-05 13:34:52 -050052 This is very important. Assigning by reference allows you to use the
53 original CodeIgniter object rather than creating a copy of it.
Andrey Andreev16a704c2012-11-09 17:25:00 +020054
vlakoff7201c9b2015-01-25 16:09:58 +010055Furthermore, if you'll be using ``get_instance()`` inside another class,
Andrey Andreev16a704c2012-11-09 17:25:00 +020056then it would be better if you assign it to a property. This way, you
57won't need to call ``get_instance()`` in every single method.
58
59Example::
60
vlakoff024cfec2013-01-09 18:10:20 +010061 class Example {
Andrey Andreev16a704c2012-11-09 17:25:00 +020062
vlakoff024cfec2013-01-09 18:10:20 +010063 protected $CI;
Andrey Andreev16a704c2012-11-09 17:25:00 +020064
vlakoff024cfec2013-01-09 18:10:20 +010065 // We'll use a constructor, as you can't directly call a function
66 // from a property definition.
67 public function __construct()
68 {
69 // Assign the CodeIgniter super-object
70 $this->CI =& get_instance();
71 }
72
73 public function foo()
74 {
75 $this->CI->load->helper('url');
76 redirect();
77 }
78
79 public function bar()
80 {
vlakoff891855d2014-09-20 08:48:27 +020081 $this->CI->config->item('base_url');
vlakoff024cfec2013-01-09 18:10:20 +010082 }
83
Andrey Andreev16a704c2012-11-09 17:25:00 +020084 }
85
Andrey Andreev16a704c2012-11-09 17:25:00 +020086In the above example, both methods ``foo()`` and ``bar()`` will work
87after you instantiate the Example class, without the need to call
88``get_instance()`` in each of them.