【问题标题】:spl_autoload Class not found following namespaces directory structurespl_autoload 在命名空间目录结构后找不到类
【发布时间】:2013-06-11 09:20:03
【问题描述】:

我正在尝试在我的项目中使用类自动加载。这是我写的:

在 main.php 上:

namespace myproject;

spl_autoload_extensions(".php");
spl_autoload_register();

subspace\myclass::mystaticmethod();

关于子空间/myclass.php:

namespace myproject\subspace;

class myclass {

    static function mystaticmethod() {

        ....

    }

}

我收到此错误:

 PHP Fatal error:  Class 'myproject\\subspace\\myclass' not found

到目前为止,我只在文档中发现了区分大小写名称的问题,所以我只切换到小写,但我得到了同样的错误。为什么?

[编辑]使用 PHP 5.3.3

【问题讨论】:

    标签: php namespaces autoload


    【解决方案1】:

    我发现了问题:默认包含目录将是完整的namespace/subspace 路径。

    我也找到了一个很好的解决方案。来自http://www.php.net/manual/en/function.spl-autoload.php#92767

    <?php
        // Add your class dir to include path
        set_include_path(get_include_path().PATH_SEPARATOR.'..');
    
        // Make autoloader look for commonly used "myclass.php" type filenames
        spl_autoload_extensions('.php');
    
        // Use default autoload implementation
        spl_autoload_register();
    ?>
    

    默认的自动加载实现是用 C 编写的,并且总是比 PHP 快一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多