Created
November 28, 2017 18:21
-
-
Save itsmikita/8855715a1eb9add9b8c1831565a202dc to your computer and use it in GitHub Desktop.
PSR-4 compliant autoloader for PHP7+
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 | |
/** | |
* PSR-4 compliant autoloader, updated for PHP7+ | |
* | |
* @param $class | |
*/ | |
spl_autoload_register( function( $class ) { | |
require "vendor/autoload.php"; | |
if( defined( 'NAMESPACE_PREFIX' ) ) { | |
$vendors[ NAMESPACE_PREFIX ] = "src"; | |
} | |
foreach( $vendors as $prefix => $path ) { | |
if( 0 === strpos( $class, $prefix ) ) { | |
$basedir = __DIR__ . $path; | |
$file = "{$basedir}/" . str_replace( "\\", "/", $class ) . ".php"; | |
if( is_readable( $file ) ) { | |
require $file; | |
break; | |
} | |
} | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment