Welcome Guest, Not a member yet? Register   Sign In
How to load my classes whenever, wherever without having to include/require them
#1

[eluser]sneakyimp[/eluser]
So it's been awhile since I set up a new CodeIgniter project and I'm wondering one big fat thing: how/where do I create an autoloader function such that my classes will be located correctly without me having to specifically include/require the file that defines them?

I looked at one of my old projects and saw that I had put this code at the end of the /config/config.php file:
Code:
function my_autoload($class) {
$path = array('core', 'libraries', 'models');
if(strpos($class, 'CI_') !== 0) {
  foreach($path as $dir) {
   $full_path = APPPATH . $dir . '/' . $class . '.php';
   if (file_exists($full_path))
    include_once($full_path);
  }
}
}
spl_autoload_register('my_autoload');

A couple of things concern me:
1) This is an odd place to put a function, no? I vaguely recall that it was necessary to define the autoload function sufficiently early that it properly located my classes for use in a controller.
2) If I need to upgrade CodeIgniter, will this be preserved?
3) Is there some other, more 'correct' way to do this?



Messages In This Thread
How to load my classes whenever, wherever without having to include/require them - by El Forum - 05-01-2014, 05:46 PM



Theme © iAndrew 2016 - Forum software by © MyBB