使用spl_autoload_register时,在根目录中找不到类

时间:2015-02-17 12:30:37

标签: php autoload spl-autoload-register

我使用spl_autoload_register进行类自动加载,如bellow

spl_autoload_register(array($this, 'mainLoader'));

function mainLoader($class) {
    $dirs = explode(CLASS_SEPARATOR, $class);
    $dirsLen = count($dirs);
    $class_name = $dirs[$dirsLen - 1];
    if ($dirsLen > 1) {
      $paths = array_slice($dirs, 0, $dirsLen - 1);
      $path = ROOT . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, $paths) . DIRECTORY_SEPARATOR;
      set_include_path(get_include_path() . PATH_SEPARATOR . $path);
    } else {
      set_include_path(get_include_path() . PATH_SEPARATOR . ROOT);
    }
    spl_autoload_extensions(".php");
    spl_autoload($class_name);
  }

输入类名可以是class,dir_class,dir_dir_class,... 和文件可以是ROOT / class.php Root / dir / class.php,Root / dir / dir / class.php,... 但是当我向我运行程序错误时

Fatal error:  Class 'class' not found in ...

为什么我的自动加载程序无法正常工作?

注意:此功能在Windows中运行良好但在Linux Ubuntu 14.04中不起作用

1 个答案:

答案 0 :(得分:0)

您可能正在将camelCase名称用于您的文件。 Unix在文件名中区分大小写,而spl_autoload只适用于小写。

相关问题