views:

46

answers:

2

Hello,

Please how can I use spl_autoload_register() with Codeigniter? I need to do this because Im using Codeigniter with another framework which also uses autoload.

I saw something here

http://stackoverflow.com/questions/3710480/php-spl-autoload-register

but I dont know how to target the CodeIgniter autoload. Im new to OOP and Codeigniter. Thanks a lot!

The above link has this:


function autoload_services($class_name){
    $file = 'services/' . $class_name. '.php';
    if (file_exists($file)){
        require_once($file);
    }
}

function autoload_vos($class_name){
    $file = 'vos/' . $class_name. '.php';
    if (file_exists($file)){
        require_once($file);
    }
}

function autoload_printers($class_name){
    $file = 'printers' . $class_name. '.php';
    if (file_exists($file)){
        require_once($file);
    }
}

spl_autoload_register('autoload_services');
spl_autoload_register('autoload_vos');
spl_autoload_register('autoload_printers');
A: 

Ahhh, I see now (after looking at a prior question you asked)... You're having a problem because there are 2 defined __autoload functions (and hence result in a parse error)...

To fix it, simply rename one of them to something else, and then right after definition call spl_autoload_register('yournewfunctionname');...

That should be all there is to it, so long as I understand your problem...

ircmaxell
Thanks for your reply, ircmaxwell. I renamed the flourish autoload function to ___autoload_flourish and after definition I called spl_autoload_register('___autoload_flourish'); but no, its not working still. I get the very same errors. I think I will need to understand how CI does its own autoloading cos it is not the usual PHP autoload function :( What do you think please?
Ticabo
Does either throw an exception or issue an error if it can't load the class? Can you post the code of both functions?
ircmaxell
I got it working fine now, thanks ircmaxwell!
Ticabo
A: 

Thanks to http://codeigniter.com/forums/viewthread/73804/#366081 and some bits of information from some CI folk that I follow on twitter (I asked em): Eric Barnes, Dan Horrigan, Phil Sturgeon and Zack Kitzmiller, I found a solution. If you are a CodeIgniter n00b like me, you may like to follow these guys.

I deleted init.php and config.php, then jammed the following into the bottom of my CI's config.php (I am also autoloading from a custom library called mylibrary).

function multi_auto_require($class) {
if(stripos($class, 'CI') === FALSE && stripos($class, 'PEAR') === FALSE) {
    foreach (array('flourish', 'mylibrary') as $folder){
        if (is_file(APPPATH."../auxengines/{$folder}/{$class}.php")){
            include_once APPPATH."../auxengines/{$folder}/{$class}.php";
        }
    }
}
}

spl_autoload_register('multi_auto_require');

Works brilliantly. Thanks, people!

Ticabo