ruk·si

🐘 PHP
Namespace Autoloader

Updated at 2013-07-04 10:34

You should use this autoloader to standardize your namespaces.

function autoload($className) {
    $className = ltrim($className, '\\');
    $fileName  = '';
    $namespace = '';
    $lastNsPos = strrpos($className, '\\')
    if ( $lastNsPos ) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\\',DIRECTORY_SEPARATOR,$namespace);
        $filename .= DIRECTORY_SEPARATOR;
    }
    $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
    require_once $fileName;
}

Source