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 | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 12 | .. php:function:: get_instance() |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 13 | |
Andrey Andreev | b011716 | 2015-03-21 12:18:10 +0200 | [diff] [blame] | 14 | :returns: Reference to your controller's instance |
| 15 | :rtype: CI_Controller |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 16 | |
| 17 | **Any class that you instantiate within your controller methods can |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 18 | access CodeIgniter's native resources** simply by using the |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 19 | ``get_instance()`` function. This function returns the main |
| 20 | CodeIgniter object. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 21 | |
Andrey Andreev | fffdd9c | 2017-06-15 11:27:31 +0300 | [diff] [blame] | 22 | Normally, to call any of the available methods, CodeIgniter requires |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 23 | you to use the ``$this`` construct:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 24 | |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 25 | $this->load->helper('url'); |
| 26 | $this->load->library('session'); |
| 27 | $this->config->item('base_url'); |
| 28 | // etc. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 29 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 30 | ``$this``, however, only works within your controllers, your models, |
| 31 | or your views. If you would like to use CodeIgniter's classes from |
| 32 | within your own custom classes you can do so as follows: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 33 | |
| 34 | First, assign the CodeIgniter object to a variable:: |
| 35 | |
| 36 | $CI =& get_instance(); |
| 37 | |
| 38 | 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] | 39 | *instead* of ``$this``:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 40 | |
Derek Jones | af8da30 | 2011-10-05 17:40:07 -0500 | [diff] [blame] | 41 | $CI =& get_instance(); |
| 42 | |
| 43 | $CI->load->helper('url'); |
| 44 | $CI->load->library('session'); |
| 45 | $CI->config->item('base_url'); |
| 46 | // etc. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 47 | |
Andrey Andreev | b011716 | 2015-03-21 12:18:10 +0200 | [diff] [blame] | 48 | If you'll be using ``get_instance()`` inside another class, then it would |
| 49 | be better if you assign it to a property. This way, you won't need to call |
| 50 | ``get_instance()`` in every single method. |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 51 | |
| 52 | Example:: |
| 53 | |
vlakoff | 024cfec | 2013-01-09 18:10:20 +0100 | [diff] [blame] | 54 | class Example { |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 55 | |
vlakoff | 024cfec | 2013-01-09 18:10:20 +0100 | [diff] [blame] | 56 | protected $CI; |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 57 | |
vlakoff | 024cfec | 2013-01-09 18:10:20 +0100 | [diff] [blame] | 58 | // 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 | { |
vlakoff | 891855d | 2014-09-20 08:48:27 +0200 | [diff] [blame] | 74 | $this->CI->config->item('base_url'); |
vlakoff | 024cfec | 2013-01-09 18:10:20 +0100 | [diff] [blame] | 75 | } |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 78 | In the above example, both methods ``foo()`` and ``bar()`` will work |
| 79 | after you instantiate the Example class, without the need to call |
Andrey Andreev | b011716 | 2015-03-21 12:18:10 +0200 | [diff] [blame] | 80 | ``get_instance()`` in each of them. |