Skip to content

Instantly share code, notes, and snippets.

@inhere
Last active June 27, 2019 10:41
Show Gist options
  • Save inhere/dba21990e370f82eb0241182c4c997f5 to your computer and use it in GitHub Desktop.
Save inhere/dba21990e370f82eb0241182c4c997f5 to your computer and use it in GitHub Desktop.
quick add an pacakge autoloader for php
<?php
$libDir = __DIR__ . '/';
$npMap = [
'SwoftTool\\' => $libDir,
// 'Inhere\\ValidateTest\\' => $libDir . '/test/',
];
spl_autoload_register(function ($class) use ($npMap) {
foreach ($npMap as $np => $dir) {
if (strpos($class, $np) !== 0) {
continue;
}
$file = $dir . str_replace('\\', '/', substr($class, strlen($np))) . '.php';
if (file_exists($file)) {
include $file;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment