php - PSR-0 自动加载器 : Namespaces and the directory structure

标签 php namespaces autoload autoloader psr-0

PSR-0 自动加载器约定是否需要使用映射到目录结构的命名空间?

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 需要它。

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





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

关于php - PSR-0 自动加载器 : Namespaces and the directory structure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17420703/

相关文章:

php - 使所有单词小写,每个单词的第一个字母大写

php - Application.cpp :422: Mismatch between target UID (584) and UID (99) of file 中的 SoftException

symfony - Doctrine 的命名空间别名

c++ - 命名空间 + 函数与类上的静态方法

php - Composer自动加载完整示例?

PHP 在类名前加上子空间用于自动加载

javascript - 如何以 php,html 多选项卡形式传递数据?

php - 与字段无关的无效消息

xml - 如何修复 : Attribute xmlns:gco redefined

php - 我应该在哪里编写代码以便 Composer 可以自动加载我的 PHP 类?