namespaces[$namespace][] = $path.'/'; } } } /** * Add "loadClass" method as an autoload handler. * * @return bool */ public function register(): bool { return spl_autoload_register([$this, 'loadClass']); } /** * Attempts to find and load the given class. * * @param string $class_name * * @return bool */ protected function loadClass(string $class_name): bool { $chunks = explode('\\', $class_name); $file_name = array_pop($chunks).'.php'; do { $namespace = implode('\\', $chunks); if (array_key_exists($namespace, $this->namespaces)) { foreach ($this->namespaces[$namespace] as $dir) { if (is_file($dir.$file_name)) { require $dir.$file_name; return true; } } } if ($chunks) { $file_name = strtolower(array_pop($chunks)).'/'.$file_name; } } while ($chunks); return false; } }