Skip to content

Instantly share code, notes, and snippets.

@thiphariel
Created September 28, 2014 08:42
Show Gist options
  • Select an option

  • Save thiphariel/ee08db39c53c0294979b to your computer and use it in GitHub Desktop.

Select an option

Save thiphariel/ee08db39c53c0294979b to your computer and use it in GitHub Desktop.
Light autoloader
<?php
/**
* Autoloader class
* @author Thomas Collot
*/
namespace Light\App;
class Autoload
{
/**
* Register the autoloader
*/
public function register()
{
spl_autoload_register(array(__CLASS__, 'autoload'));
}
/**
* Light Autoload
*/
private function autoload($class)
{
$class = ltrim($class, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($class, '\\')) {
$namespace = substr($class, 0, $lastNsPos);
$class = substr($class, $lastNsPos + 1);
$fileName = strtolower(str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR);
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
$fileName = strtolower(substr($fileName, 6));
require_once '../' . $fileName;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment