In my autoload file I have several common libraries and several common model autoloaded:
$autoload['libraries'] = array('database','session','tools','common'....);
$autoload['model'] = array('mauth','madmin',...);
I’m in calling this code inside a library that calls another library:
class library1{
function x(){
$this->CI =& get_instance();
$this->CI->library2->y();
}
}
class library2()
function y(){
$this->CI =& get_instance();
$array_notifications = $this->CI->madmin->getNotifications();
}
}
And I get:
Message: Undefined property: Splash::$madmin
I guess I may do this:
$this->CI->load->model('madmin');
But.. why? Why Can’t I decide the load priority of my autoloaded classes?
Thanks
EDIT: Clarification
AFAIK there’s no possibility of choosing the loading priority, and I think that hacking the core just to avoid writing a line is a bit…off.
I had to go search the core system to see better how things work. Looks like libraries are autoloaded before models, so you are right when saying that you need to load the model before calling one of its methods (I didn’t tested though, but I think it works like this).
I don’t know why is this done, but so far the only solution I see is to change yourself the order in which are loaded, but I warn you: I think that if things work this way there’s a reason the CI developers has thought about; this might have drawbacks you cannot tell, so always be ready to a drawback in case you find problems.
This solution is just a bit speculative and not guaranteed, I’ll appreciate your feedback as I cannot set up a testing environment right now to prove it
Go to the
system/core/loader.phpfile, around line 1166 (the method is_ci_autoloader()) where the “libraries” index of$autoloadarray is checked, and swap its position with the ‘model’ index checking which is around line 1183.And good luck 🙂