Skip to content

Instantly share code, notes, and snippets.

@mkgeeky
Last active May 6, 2025 11:21
Show Gist options
  • Save mkgeeky/f389e720128013c74f1e9285289ea13b to your computer and use it in GitHub Desktop.
Save mkgeeky/f389e720128013c74f1e9285289ea13b to your computer and use it in GitHub Desktop.
Own autoloader with multiple directorys
<?php
class Autoloader
{
public static function register()
{
spl_autoload_register(function ($class) {
$ff = str_replace('\\', DIRECTORY_SEPARATOR, $class);
$Class_Dirs = array (
"/Class/",
"/Controllers/",
"/Controllers/Content/",
);
for ($i = 0; $i < count($Class_Dirs); $i++)
{
$file = $_SERVER["DOCUMENT_ROOT"].$Class_Dirs[$i].$ff.'.php';
if (file_exists($file))
{
require $file;
return true;
}
}
return false;
});
}
}
Autoloader::register();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment