If you use in your project namespaces i would recommend to use simple code:
class ClassLoader {
public function handle($class) {
$file = str_replace('\\', '/', $class.'.php');
if(!file_exists($file)){
throw new \Exception('class '.$class.' file not exists');
}
include_once $file;
}
}
$autloader = new Classes\ClassLoader;
spl_autoload_register(array($autloader, 'handle'));
// from now you can load all Classes from directories specified in namespace of class for example
/////////////////////////////////////////////
// directory => framework/classes/User.php //
/////////////////////////////////////////////
namespace framework\classes;
class User {
public function helloWorld(){
echo 'hello World';
}
}
////////////////////////
// and here index.php //
////////////////////////
$autloader = new Classes\ClassLoader;
spl_autoload_register(array($autloader, 'handle'));
$user = new \framework\classes\User();
$user->helloWorld();