Welcome Guest, Not a member yet? Register   Sign In
Adding simple class
#12

[eluser]nocash[/eluser]
You're right, the autoload function is the way to go. I wasn't writing any code for a while but I looked into once I started up again and I've been using it since.

And for anyone that doesn't know, info on the autoload function can be found here.

I've just been making an autoload_helper.php file that contains the code below, then include the helper in autoload.cfg. It seems to work out pretty well. Hopefully that's not abuse of a helper.

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

function __autoload($class)
{
    if ( $class == 'Model' || strpos($class, 'CI_') !== false )
    {
        return;
    }
    
    $path = APPPATH.'classes/'.strtolower($class).EXT;

    if ( file_exists($path) )
    {
        require_once($path);
        return;
    }
    
    // At this point we are unable to find the class.
    log_message('error', 'Could not find file for autoload: '.$path);
}

?>

The reason for the first if statement is because I noticed a lot of native CI classes were running through my autoloader. I'm not sure what's up with that, but CI works just fine without this function's help so I put that in there.

So, I know this thread is a bit dated now, but I wanted to put this out here in case someone starting out finds it useful.


Messages In This Thread
Adding simple class - by El Forum - 01-28-2008, 11:17 AM
Adding simple class - by El Forum - 01-28-2008, 12:55 PM
Adding simple class - by El Forum - 01-28-2008, 01:19 PM
Adding simple class - by El Forum - 01-28-2008, 01:22 PM
Adding simple class - by El Forum - 01-28-2008, 05:16 PM
Adding simple class - by El Forum - 01-28-2008, 06:40 PM
Adding simple class - by El Forum - 01-28-2008, 07:00 PM
Adding simple class - by El Forum - 02-05-2008, 09:04 PM
Adding simple class - by El Forum - 02-11-2008, 12:42 AM
Adding simple class - by El Forum - 02-11-2008, 02:10 AM
Adding simple class - by El Forum - 02-11-2008, 05:06 AM
Adding simple class - by El Forum - 04-13-2008, 11:46 PM



Theme © iAndrew 2016 - Forum software by © MyBB