diff options
author | Andrey Andreev <narf@devilix.net> | 2018-06-12 15:45:46 +0200 |
---|---|---|
committer | Andrey Andreev <narf@devilix.net> | 2018-06-12 15:45:46 +0200 |
commit | 30e2eafa86c4c7b6b39cea3e7089a90df9f603fb (patch) | |
tree | 391bc1e62d8d0ad045e18a6da72e3e2a59e91503 /user_guide_src/source/general/ancillary_classes.rst | |
parent | a9da3dd2f16a8f97d7bc4ff5572b28e4bb84c813 (diff) |
[ci skip] 3.1.9 release
Diffstat (limited to 'user_guide_src/source/general/ancillary_classes.rst')
-rw-r--r-- | user_guide_src/source/general/ancillary_classes.rst | 80 |
1 files changed, 0 insertions, 80 deletions
diff --git a/user_guide_src/source/general/ancillary_classes.rst b/user_guide_src/source/general/ancillary_classes.rst deleted file mode 100644 index 4d6528aae..000000000 --- a/user_guide_src/source/general/ancillary_classes.rst +++ /dev/null @@ -1,80 +0,0 @@ -########################## -Creating Ancillary Classes -########################## - -In some cases you may want to develop classes that exist apart from your -controllers but have the ability to utilize all of CodeIgniter's -resources. This is easily possible as you'll see. - -get_instance() -============== - -.. php:function:: get_instance() - - :returns: Reference to your controller's instance - :rtype: CI_Controller - -**Any class that you instantiate within your controller methods can -access CodeIgniter's native resources** simply by using the -``get_instance()`` function. This function returns the main -CodeIgniter object. - -Normally, to call any of the available methods, CodeIgniter requires -you to use the ``$this`` construct:: - - $this->load->helper('url'); - $this->load->library('session'); - $this->config->item('base_url'); - // etc. - -``$this``, however, only works within your controllers, your models, -or your views. If you would like to use CodeIgniter's classes from -within your own custom classes you can do so as follows: - -First, assign the CodeIgniter object to a variable:: - - $CI =& get_instance(); - -Once you've assigned the object to a variable, you'll use that variable -*instead* of ``$this``:: - - $CI =& get_instance(); - - $CI->load->helper('url'); - $CI->load->library('session'); - $CI->config->item('base_url'); - // etc. - -If you'll be using ``get_instance()`` inside another class, then it would -be better if you assign it to a property. This way, you won't need to call -``get_instance()`` in every single method. - -Example:: - - class Example { - - protected $CI; - - // We'll use a constructor, as you can't directly call a function - // from a property definition. - public function __construct() - { - // Assign the CodeIgniter super-object - $this->CI =& get_instance(); - } - - public function foo() - { - $this->CI->load->helper('url'); - redirect(); - } - - public function bar() - { - $this->CI->config->item('base_url'); - } - } - -In the above example, both methods ``foo()`` and ``bar()`` will work -after you instantiate the Example class, without the need to call -``get_instance()`` in each of them. |