Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add autoload-function to CodeIgniter?

I would like to be able to use OOP and create new objects in my controllers in CodeIgniter. So I need to use an autoload-function:

function __autoload( $classname )
{
    require_once("../records/$classname.php");
} 

But how can I add that to CodeIgniter?

like image 636
Jonas Avatar asked May 31 '11 13:05

Jonas


1 Answers

You can add your auto loader above to app/config/config.php. I've used a similar autoload function before in this location and it's worked quite neatly.

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

Courtesy of Phil Sturgeon. This way may be more portable. core would probably be records for you; but check your paths are correct regardless. This method also prevents any interference with loading CI_ libs (accidentally)

like image 178
Ross Avatar answered Oct 10 '22 07:10

Ross