blob: 4d6528aae0750c16730e56646d6426c42e1131bf [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
Andrey Andreevb0117162015-03-21 12:18:10 +020014 :returns: Reference to your controller's instance
15 :rtype: CI_Controller
Andrey Andreev16a704c2012-11-09 17:25:00 +020016
17**Any class that you instantiate within your controller methods can
Derek Jones8ede1a22011-10-05 13:34:52 -050018access CodeIgniter's native resources** simply by using the
Andrey Andreev16a704c2012-11-09 17:25:00 +020019``get_instance()`` function. This function returns the main
20CodeIgniter object.
Derek Jones8ede1a22011-10-05 13:34:52 -050021
Andrey Andreevfffdd9c2017-06-15 11:27:31 +030022Normally, to call any of the available methods, CodeIgniter requires
Andrey Andreev16a704c2012-11-09 17:25:00 +020023you to use the ``$this`` construct::
Derek Jones8ede1a22011-10-05 13:34:52 -050024
Derek Jonesaf8da302011-10-05 17:40:07 -050025 $this->load->helper('url');
26 $this->load->library('session');
27 $this->config->item('base_url');
28 // etc.
Derek Jones8ede1a22011-10-05 13:34:52 -050029
Andrey Andreev16a704c2012-11-09 17:25:00 +020030``$this``, however, only works within your controllers, your models,
31or your views. If you would like to use CodeIgniter's classes from
32within your own custom classes you can do so as follows:
Derek Jones8ede1a22011-10-05 13:34:52 -050033
34First, assign the CodeIgniter object to a variable::
35
36 $CI =& get_instance();
37
38Once you've assigned the object to a variable, you'll use that variable
Andrey Andreev16a704c2012-11-09 17:25:00 +020039*instead* of ``$this``::
Derek Jones8ede1a22011-10-05 13:34:52 -050040
Derek Jonesaf8da302011-10-05 17:40:07 -050041 $CI =& get_instance();
42
43 $CI->load->helper('url');
44 $CI->load->library('session');
45 $CI->config->item('base_url');
46 // etc.
Derek Jones8ede1a22011-10-05 13:34:52 -050047
Andrey Andreevb0117162015-03-21 12:18:10 +020048If you'll be using ``get_instance()`` inside another class, then it would
49be better if you assign it to a property. This way, you won't need to call
50``get_instance()`` in every single method.
Andrey Andreev16a704c2012-11-09 17:25:00 +020051
52Example::
53
vlakoff024cfec2013-01-09 18:10:20 +010054 class Example {
Andrey Andreev16a704c2012-11-09 17:25:00 +020055
vlakoff024cfec2013-01-09 18:10:20 +010056 protected $CI;
Andrey Andreev16a704c2012-11-09 17:25:00 +020057
vlakoff024cfec2013-01-09 18:10:20 +010058 // We'll use a constructor, as you can't directly call a function
59 // from a property definition.
60 public function __construct()
61 {
62 // Assign the CodeIgniter super-object
63 $this->CI =& get_instance();
64 }
65
66 public function foo()
67 {
68 $this->CI->load->helper('url');
69 redirect();
70 }
71
72 public function bar()
73 {
vlakoff891855d2014-09-20 08:48:27 +020074 $this->CI->config->item('base_url');
vlakoff024cfec2013-01-09 18:10:20 +010075 }
Andrey Andreev16a704c2012-11-09 17:25:00 +020076 }
77
Andrey Andreev16a704c2012-11-09 17:25:00 +020078In the above example, both methods ``foo()`` and ``bar()`` will work
79after you instantiate the Example class, without the need to call
Andrey Andreevb0117162015-03-21 12:18:10 +020080``get_instance()`` in each of them.