CodeIgniter Forums
always check if a class has been previously loaded ? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: always check if a class has been previously loaded ? (/showthread.php?tid=61428)



always check if a class has been previously loaded ? - djsuperfive - 04-15-2015

Hi,

Can we use $this->load (library, model, or other loadable class or item) without making a check if that item has been already previously loaded with $this->load->is_loaded() ?

Is it the same for when using $this->lang-load ?

thanks.


RE: always check if a class has been previously loaded ? - CroNiX - 04-15-2015

CI (in Loader class) already makes a check and won't load something if it's already been loaded so no need to check it manually. It will just return the previously loaded instance if it's already been loaded instead of reloading it.


RE: always check if a class has been previously loaded ? - mwhitney - 04-20-2015

Generally, $this->load->xyz() and $this->lang->load() are fairly intelligent about checking whether something has been loaded. For example, if you're loading a language file which has previously been loaded, it won't be reloaded. However, if you're loading a language file which was loaded previously under a different language, it will reload the file for the new language.

However, you may be able to perform a more efficient check in your code than that used by the method in question and save yourself a small amount of time for subsequent calls to load the same model or library. It's not necessarily worth the effort, because $this->load->model() and $this->load->library() should never cause any problems by being called for a model or library which has already been loaded, but it could make a small difference in extreme cases.