Skip to content

Instantly share code, notes, and snippets.

@mageekguy
Last active July 8, 2020 19:48
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mageekguy/8300961 to your computer and use it in GitHub Desktop.
Save mageekguy/8300961 to your computer and use it in GitHub Desktop.
Very simple PSR-4 autoloader
<?php
namespace your\namespace\here;
spl_autoload_register(function($class) {
if (stripos($class, __NAMESPACE__) === 0)
{
@include(__DIR__ . DIRECTORY_SEPARATOR . 'classes' . str_replace('\\', DIRECTORY_SEPARATOR, strtolower(substr($class, strlen(__NAMESPACE__)))) . '.php');
}
}
);
// Just put this file in the root directory of your project and include it in your bootstrap file and update the namespace.
// File name should be lowercase.
// File name should have .php extension, if you want using an another extension, update line 8 accordingly.
// Search class in "classes" directory, if you want using an another directory, update line 8 accordingly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment