Welcome Guest, Not a member yet? Register   Sign In
classes autoloading in CI
#1

[eluser]nourdine[/eluser]
hello people

I was after a way to manage auto inclusion of my own classes (not controllers or models) in CI without clashing with the way CI includes its own files. I came up with this simple utility:

Code:
class CLoader {

   static public $root = "";

   static public $foldersDescription = array(
       "application/bean/authentication/",
       "application/bean/form/",
       // etc etc ...
   );

   static public function getFilename($className, $folder) {
      return self::$root . $folder . $className . ".php";
   }

   public static function init() {

      if (!function_exists("__autoload")) {

         self::$root = dirname(__FILE__); // this file has to be placed in the root of the project (where index.php lives)

         function __autoload($class) {
            // I don't load CI classes as the framework handle their inclusion themselves
            if (stristr($class, "ci_") || stristr($class, "my_")) {
               return;
            }
            foreach (FunkyLoader::$foldersDescription as $folder) {
               $file = FunkyLoader::getFilename($class, $folder);
               if (file_exists($file)) {
                  include_once($file);
                  return;
               }
            }
         }
      }
   }
}

comments? ideas? like it?




Theme © iAndrew 2016 - Forum software by © MyBB