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

[eluser]Seppo[/eluser]
To simple include a file you can use $this->load->file method.
#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.




Theme © iAndrew 2016 - Forum software by © MyBB