Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ########################## |
| 2 | Creating Ancillary Classes |
| 3 | ########################## |
| 4 | |
| 5 | In some cases you may want to develop classes that exist apart from your |
| 6 | controllers but have the ability to utilize all of CodeIgniter's |
| 7 | resources. This is easily possible as you'll see. |
| 8 | |
| 9 | get_instance() |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame^] | 10 | ============== |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 11 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame^] | 12 | .. php:function:: get_instance() |
| 13 | |
| 14 | :returns: object of class CI_Controller |
| 15 | |
| 16 | **Any class that you instantiate within your controller methods can |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 17 | access CodeIgniter's native resources** simply by using the |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame^] | 18 | ``get_instance()`` function. This function returns the main |
| 19 | CodeIgniter object. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 20 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame^] | 21 | Normally, to call any of the available CodeIgniter methods requires |
| 22 | you to use the ``$this`` construct:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 23 | |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 24 | $this->load->helper('url'); |
| 25 | $this->load->library('session'); |
| 26 | $this->config->item('base_url'); |
| 27 | // etc. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 28 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame^] | 29 | ``$this``, however, only works within your controllers, your models, |
| 30 | or your views. If you would like to use CodeIgniter's classes from |
| 31 | within your own custom classes you can do so as follows: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 32 | |
| 33 | First, assign the CodeIgniter object to a variable:: |
| 34 | |
| 35 | $CI =& get_instance(); |
| 36 | |
| 37 | Once you've assigned the object to a variable, you'll use that variable |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame^] | 38 | *instead* of ``$this``:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 39 | |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 40 | $CI =& get_instance(); |
| 41 | |
| 42 | $CI->load->helper('url'); |
| 43 | $CI->load->library('session'); |
| 44 | $CI->config->item('base_url'); |
| 45 | // etc. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame^] | 47 | .. note:: You'll notice that the above get_instance() ``function`` is being |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 48 | passed by reference:: |
| 49 | |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 50 | $CI =& get_instance(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | |
| 52 | This is very important. Assigning by reference allows you to use the |
| 53 | original CodeIgniter object rather than creating a copy of it. |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame^] | 54 | |
| 55 | Furthermore, if you'll be using ``get_intance()`` inside anoter class, |
| 56 | then it would be better if you assign it to a property. This way, you |
| 57 | won't need to call ``get_instance()`` in every single method. |
| 58 | |
| 59 | Example:: |
| 60 | |
| 61 | class Example { |
| 62 | |
| 63 | protected $CI; |
| 64 | |
| 65 | // 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 | { |
| 81 | $this->CI->config_item('base_url'); |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |
| 86 | In the above example, both methods ``foo()`` and ``bar()`` will work |
| 87 | after you instantiate the Example class, without the need to call |
| 88 | ``get_instance()`` in each of them. |