Last active
March 22, 2022 02:07
-
-
Save ssi-anik/e6b25c1d681e44601393894ab3ec1ad7 to your computer and use it in GitHub Desktop.
https://medium.com/p/7b31f025ab6a - with autoloading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function autoload_src($file): bool | |
{ | |
echo sprintf('autoload_src: Looking for "%s".%s', $file, PHP_EOL); | |
if (strtolower(($extract = explode('\\', $file))[0]) === 'source') { | |
return loadFileIfExists('./src', $extract[1] ?? ''); | |
} | |
return false; | |
} | |
function autoload_app($file): bool | |
{ | |
echo sprintf('autoload_app: looking for "%s".%s', $file, PHP_EOL); | |
if (strtolower(($extract = explode('\\', $file))[0]) === 'app') { | |
return loadFileIfExists('./app', $extract[1] ?? ''); | |
} | |
return false; | |
} | |
function autoload_current($file): bool | |
{ | |
echo sprintf('autoload_current: looking for "%s".%s', $file, PHP_EOL); | |
return loadFileIfExists('.', $file); | |
} | |
function loadFileIfExists(string $directory, string $filename): bool | |
{ | |
if (file_exists($path = sprintf('%s/%s.php', rtrim($directory, '/'), $filename))) { | |
include($path); | |
return true; | |
} | |
return false; | |
} | |
spl_autoload_register('autoload_app'); | |
spl_autoload_register('autoload_src'); | |
spl_autoload_register('autoload_current', prepend: true); | |
echo sprintf('autoloader order: %s.%s', json_encode(spl_autoload_functions()), PHP_EOL); | |
//spl_autoload_unregister('autoload_two'); | |
//var_dump(spl_autoload_functions()); | |
echo (new Current())->sayHello('anik'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment