문제

Does the PSR-0 autoloader convention require using namespaces that map to the directory structure?

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

    require $fileName;
}
도움이 되었습니까?

해결책

PSR-0 require it.

Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system.

and

The fully-qualified namespace and class is suffixed with .php when loading from the file system.

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top