如何创建/使用基于命名空间的PHP自动加载程序来加载类、常量和函数?

如何创建/使用基于命名空间的PHP自动加载程序来加载类、常量和函数?,php,function,class,namespaces,constants,Php,Function,Class,Namespaces,Constants,我有以下自动加载器: spl_autoload_register(function($class) { $class = ltrim($class, '\\'); $filename = ''; $namespace = ''; if ($last_ns_pos = strripos($class, '\\')) { $namespace = strtolower(substr($class, 0, $last_ns_pos));

我有以下自动加载器:

spl_autoload_register(function($class) {
    $class = ltrim($class, '\\');
    $filename  = '';
    $namespace = '';

    if ($last_ns_pos = strripos($class, '\\')) {
        $namespace = strtolower(substr($class, 0, $last_ns_pos));
        $class = substr($class, $last_ns_pos + 1);
        $filename  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }

    $filename .= str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';

    require $filename;
});

如果我想加载MyApp\Library\ClassOne,这很好,因为该类位于/MyApp/Library/ClassOne.php中。这就实现了;但是,我希望能够自动加载常量和函数的文件,例如分别位于MyApp\constants.php和MyApp\functions.php中的MyApp\constants和MyApp\functions。

从php 5.5开始,php目前不支持自动加载除类以外的任何内容

自动加载函数和常量的功能目前正在讨论中,可能会添加到下一个版本ie PHP 5.6中,但尚未最终确定


有关更多详细信息,请参阅。

自PHP5.5起,PHP目前不支持自动加载类以外的任何内容

自动加载函数和常量的功能目前正在讨论中,可能会添加到下一个版本ie PHP 5.6中,但尚未最终确定


有关详细信息,请参阅。

Ah!所以我在尝试不可能的事情。谢谢你的RFC链接。啊!所以我在尝试不可能的事情。谢谢你的RFC链接。