I created a small working autoloader based on some of the answers and ideas found on Stack Exchange (I lost the reference). However, I don't know much about PHP (I mainly know C, a bit C++ and Java). I'm trying to create a small MVC website for school project using my own templates and JavaScript as well. I mainly created this autoloader to include class files (I'm not sure if any other files can be included with autoloader too).
Can you please check this autoloader and give your insight on what can be done better and if there are any shortcomings?
It's important to know that I don't use namespaces currently because it would overcomplicate things.
<?php
class Autoload
{
private $allFiles = array();
public function __construct() {
$this->findDir(__DIR__, $this->allFiles);
spl_autoload_register(array($this, 'classLoader'));
}
private function classLoader($class) {
if (class_exists($class, false)) {
return true;
} else {
$this->includeClass($this->allFiles, $class);
// should I return here true as well?
}
}
private function findDir($currDir, &$files) {
$dirsInCurr = glob($currDir . "/*", GLOB_ONLYDIR);
$this->findFiles($currDir, $files);
//recursive call
foreach ($dirsInCurr as $dir) $this->findDir($dir, $files);
}
private function findFiles($dir, &$files) {
$fPaths = glob($dir . "/*.php");
foreach ($fPaths as $f) $files[] = $f . "\n";
}
private function includeClass($files, $class) {
foreach ($files as $f) {
if (strpos($f, strtolower($class) . ".php")) require_once trim($f);
}
}
}
$autoload = new Autoload();
?>