From c8b0408bd55dbce0681209a6eb1fbf1330ab0661 Mon Sep 17 00:00:00 2001 From: billz Date: Tue, 25 Mar 2025 05:14:29 -0700 Subject: [PATCH] Refactor + fixup autoloader --- includes/autoload.php | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/includes/autoload.php b/includes/autoload.php index d39bd0c9..87177471 100755 --- a/includes/autoload.php +++ b/includes/autoload.php @@ -9,31 +9,13 @@ */ spl_autoload_register(function ($class) { - // project-specific namespace prefix - $prefix = ''; + // base directory where all class files are stored + $base_dir = __DIR__ . '/../src/'; - // base directory for the namespace prefix - $base_dir = 'src/'; + // convert the fully qualified class name into a file path + $file = $base_dir . str_replace('\\', '/', $class) . '.php'; - // normalize the base directory with a trailing separator - $base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/'; - - // does the class use the namespace prefix? - $len = strlen($prefix); - if (strncmp($prefix, $class, $len) !== 0) { - // no, move to the next registered autoloader - return; - } - - // get the relative class name - $relative_class = substr($class, $len); - - // replace the namespace prefix with the base directory, replace namespace - // separators with directory separators in the relative class name, append - // with .php - $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; - - // if the file exists, require it + // require the file if it exists if (file_exists($file)) { require $file; }