CodeIgniter provides a simple way to load libraries located in the application folder. Personally, I prefer to store most of the libraries in a common centralized location. Unfortunately, there is no elegant built in solution to access such external libraries. One may suggest using hooks, but that seems like too much hassle to me.
Fortunately, for those using PHP5, there is an easy way out. PHP5 offers a few really neat functions that can make your life quite a bit easier, one of them is __autoload. This function will try to load a class file if it’s not already loaded, simple.
The best place to place __autoload function is the config.php file, as it is one of the first to be included by CodeIgniter system. Here’s the config.php snippet:
function __autoload($class) {
//prevent CI and Pear classes from being loaded.
if(!strstr($class, 'CI') && !stristr($class, 'PEAR')){
require_once(PATH_TO_LIBRARIES."$class.php");
}
}
Essentially, by parsing the class name, you can create different loading routines for different libraries. After you’ve set up the __autoload function, call your classes as usual (new, extends, implements and static method calls, should all trigger __autoload).
Shadowhand says:
Even better than
__autoload()isspl_register_autoload().Shadowhand says:
Sorry, that should be
spl_autoload_register().Alex says:
Thanks for the pointer. It does, in fact, provide better and more flexible ways for autoloading. Also, there is a possibility to place all of the routines into methods within a class.
Ruben Zevallos Jr. says:
Thanks for the idea… I was trying to understand how get I add my own library…
gillbates says:
Hi Alex. It’s me again. Where would be a good place to define PATH_TO_LIBRARIES? Thanks!
-brian
Alex says:
I place all of them into index.php, seems logical since all of the other constants are created there.
gillbates says:
Thanks. Did you encounter significant slowdown in performance after using __autoload()? My app feels slower, but I didn’t bother to check loading times using the profiler before enabling autoload, so I’m not sure.
Alex says:
No, I haven’t encountered any slowdowns. I use this setup on several machines and they all seem to run it rather well.
gillbates says:
According to the codeigniter profiler, there are around 0.0589s of difference in execution time between autoloaded and autoload-less pages that are otherwise identical. The differences will be more or less noticeable depending on the complexity of the page, I suppose.
gillbates says:
However, I am running this on a very beleaguered development machine. It will probably be negligible once it goes into production.
Alex says:
Hmmm… Well, can you try loading that parent model using require_once() and see what difference that would make?
gillbates says:
Yup, it did run faster when using require_once.
I was loading my models [$this->load->model(’Subareamodel’)] in my controller constructor. Instead of doing that I loaded the models in the controller methods that needed them. This made the whole thing run at its previously quick pace, even with autoload. I can live with that, I suppose.