Skip to content

Instantly share code, notes, and snippets.

@itsmikita
Created November 28, 2017 18:21
Show Gist options
  • Save itsmikita/8855715a1eb9add9b8c1831565a202dc to your computer and use it in GitHub Desktop.
Save itsmikita/8855715a1eb9add9b8c1831565a202dc to your computer and use it in GitHub Desktop.
PSR-4 compliant autoloader for PHP7+
<?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