Last active
January 7, 2019 14:56
-
-
Save cheich/dcad11779ef279f819b5dee969c7c943 to your computer and use it in GitHub Desktop.
PSR-4 class autoloader
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 | |
namespace MyProjectNamespace; | |
/** | |
* PSR-4 class autoloader | |
* | |
* @param string $class The fully-qualified class name. | |
* | |
* @return void | |
*/ | |
spl_autoload_register(function ($class) { | |
$prefix = __NAMESPACE__ . '\\'; | |
$base_dir = __DIR__ . '/inc/'; | |
$len = strlen($prefix); | |
if (strncmp($prefix, $class, $len) !== 0) | |
return; | |
$relative_class = substr($class, $len); | |
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php'; | |
if (file_exists($file)) | |
require $file; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment