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).


12 Responses to “CodeIgniter - loading external libraries”

  • Even better than __autoload() is spl_register_autoload().

  • Sorry, that should be spl_autoload_register().

  • 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.

  • Thanks for the idea… I was trying to understand how get I add my own library…

  • Hi Alex. It’s me again. Where would be a good place to define PATH_TO_LIBRARIES? Thanks!

    -brian

  • I place all of them into index.php, seems logical since all of the other constants are created there.

  • 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.

  • No, I haven’t encountered any slowdowns. I use this setup on several machines and they all seem to run it rather well.

  • 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.

  • However, I am running this on a very beleaguered development machine. It will probably be negligible once it goes into production.

  • Hmmm… Well, can you try loading that parent model using require_once() and see what difference that would make?

  • 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.

Leave a Reply


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>