Welcome Guest, Not a member yet? Register   Sign In
Using the PHP spl_autoload_register() with Codeigniter to autoload my own classes
#1

[eluser]Ephyzy[/eluser]
I need to autoload my own classes in Codeigniter without rewriting them all as libraries (they are very many).

Please how can I do this using spl_autoload_register()?
#2

[eluser]OliverHR[/eluser]
If you write you code as oo i mean classes try using __autoload on config o register differents autoload functions there with spl_autoload_register().

Read this:
CodeIgniter Base Classes: Keeping it DRY by Phil Sturgeon
#3

[eluser]Ephyzy[/eluser]
Thanks, OliverHR. I already did that. I am using another framework which has its own autoload function called ___autoload_all. So I called spl_autoload_register("___autoload_all") right after it was defined. I still get the same errors. I think this may have something to do with CI's methods of autoloading, its not the conventional way. I was told that CodeIgniter has a different way of going about autoloading. Any help pls?
#4

[eluser]Ephyzy[/eluser]
I am using the Flourish Unframework Library (http://flourishlib.com) and after speaking to the author, this was what he recommended. But I can't seem to find how to use spl_autoload_register() with CodeIgniter's own autoload.
#5

[eluser]OliverHR[/eluser]
Codeigniter dont use __autoload because (until 1.7.2) its php 4 compatibility. Try next at the end of:

Quote:application/config/config.php

Code:
function __autoload($class_name)
{
    // Flourish directory in application/libraries
    $flourish_root = APPPATH . 'libraries/flourish/';
    
    $file = $flourish_root . $class_name . '.php';

    if (file_exists($file)) {
        include $file;
        return;
    }
    
    throw new Exception('The class ' . $class_name . ' could not be loaded');
}
#6

[eluser]Ephyzy[/eluser]
Thanks a lot, OliverHR. I tried that and it didnt work either!

Thanks to http://ellislab.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).

Code:
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!
#7

[eluser]OliverHR[/eluser]
Good for you, I try the solution that I posted and works too.




Theme © iAndrew 2016 - Forum software by © MyBB