Last active
December 28, 2015 15:48
-
-
Save abdullahbutt/7523942 to your computer and use it in GitHub Desktop.
CI_get_instance()
(get_instance() or & get_instance() in CI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| get_instance() or & get_instance() in CI: | |
| In some cases you may want to develop classes that exist apart from your controllers but have the ability to utilize all of Code Igniter's resources. This is easily possible by using get_instance() functions. | |
| Any class that you instantiate within your controller functions can access Code Igniter's native resources simply by using the get_instance() function. This function returns the main Code Igniter object. | |
| Normally, to call any of the available Code Igniter functions requires you to use the | |
| $this</b> 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 Code Igniter's classes from within your own custom classes you can do so as follows: | |
| First, assign the Code Igniter object to a variable: | |
| $obj =& get_instance(); | |
| Once you've assigned the object to a variable, you'll use that variable instead of $this: | |
| $obj =& get_instance(); | |
| $obj->load->helper('url'); | |
| $obj->load->library('session'); | |
| $obj->config->item('base_url'); | |
| etc. | |
| Note: You'll notice that the above get_instance() function is being passed by reference: | |
| $obj =& get_instance(); | |
| This is very important. Assigning by reference allows you to use the original Code Igniter object rather than creating a copy of it. | |
| You can read about this function from CI guide below: | |
| 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 Code Igniter's resources. This is easily possible as you'll see. | |
| get_instance() | |
| Any class that you instantiate within your controller functions can access Code Igniter's native resources simply by using the get_instance() function. This function returns the main Code Igniter object. | |
| Normally, to call any of the available Code Igniter functions 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 Code Igniter's classes from within your own custom classes you can do so as follows: | |
| First, assign the Code Igniter object to a variable: | |
| $obj =& get_instance(); | |
| Once you've assigned the object to a variable, you'll use that variable instead of $this: | |
| $obj =& get_instance(); | |
| $obj->load->helper('url'); | |
| $obj->load->library('session'); | |
| $obj->config->item('base_url'); | |
| etc. | |
| Note: You'll notice that the above get_instance() function is being passed by reference: | |
| $obj =& get_instance(); | |
| This is very important. Assigning by reference allows you to use the original Code Igniter object rather than creating a copy of it. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment