Welcome Guest, Not a member yet? Register   Sign In
Using CI() in base controller question
#1

[eluser]RJ[/eluser]
I was looking at the pyro cms source and noticed a method called "ci()" in the MY_Controller file.

Code:
function ci()
  {
    return get_instance();
  }

The method appears to be used in the settings.php file and a helper file. I am using Phil's config file addition for auto-loading to hopefully achieve more "dry" coding.

Code:
function __autoload($class)
{
    if(strpos($class, 'CI_') !== 0)
    {
        @include_once( APPPATH . 'libraries/'. $class . EXT );
    }
}


How can this method be used as
Code:
ci()->ion_auth->get_user($user)
as apposed to
Code:
$CI =& get_instance();
$CI->ion_auth->get_user($user);

I don't follow how codeigniter allows or recognizes the ci() method in auto-loaded MY_controller permitting its use in this fashion.

I am asking here because this more of a general framework/coding question then a pyro question. (Note: I am not using Pyro, I am gaining knowledge from reading their code. thanks Phil!)
#2

[eluser]TaylorOtwell[/eluser]
He has the ci() function defined outside of the MY_Controller class. It is just sitting in that file. MY_Controller will be loaded on every request, so that file (hence, that function) will also be included. Since the function is outside of any class scope, it is available globally. It's just a nifty shortcut for accessing the CI "super object".
#3

[eluser]RJ[/eluser]
I see, because the controller is pre-loaded, methods defined are available globally as methodname() rather than $this->method().

Searching the entire project for CI() locates usage in Settings.php and user_helper.php. I don't see it defined elsewhere, do you?

Thanks Taylor




Theme © iAndrew 2016 - Forum software by © MyBB