Skip to content

Instantly share code, notes, and snippets.

@igor822
Created February 13, 2014 16:54
Show Gist options
  • Save igor822/8979017 to your computer and use it in GitHub Desktop.
Save igor822/8979017 to your computer and use it in GitHub Desktop.
Simple autoload
<?php
if (!defined('BASE_PATH')) define('BASE_PATH', realpath(dirname(__FILE__))); // Root path folder to load classes
function autoloader($class_name) {
$class_name = ltrim($class_name, '\\');
$file_name = '';
$namespace = '';
if ($lastNsPos = strrpos($class_name, '\\')) {
$namespace = substr($class_name, 0, $lastNsPos);
$class_name = substr($class_name, $lastNsPos + 1);
$file_name = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR;
}
$file_name .= str_replace('_', DIRECTORY_SEPARATOR, $class_name).'.php';
if (is_readable(BASE_PATH.'/'.$file_name)) {
require_once BASE_PATH.'/'.$file_name;
}
if (!class_exists($namespace.'\\'.$class_name, false) && !interface_exists($namespace.'\\'.$class_name, false)) {
trigger_error('Unable to load class: '.$class_name, E_USER_WARNING);
}
}
spl_autoload_register('autoloader');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment