Skip to content

Instantly share code, notes, and snippets.

@julianwachholz
Created October 7, 2011 19:53
Show Gist options
  • Save julianwachholz/1271217 to your computer and use it in GitHub Desktop.
Save julianwachholz/1271217 to your computer and use it in GitHub Desktop.
A namespace based autoloader
<?php
spl_autoload_register(function($class)
{
$path = str_replace('\\', DIRECTORY_SEPARATOR, $class);
// Convert the PascalCased names to snake_case
$path = preg_replace('/([a-z])([A-Z](?![A-Z]))/', '$1_$2', $path);
$path = strtolower($path);
$path = LIB_PATH . '/' . $path . '.php';
require($path);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment