由于找不到文件,PHP自动加载失败


PHP autoloading failing due to file not being found

我在GoDaddy共享主机帐户上托管。我的绝对托管路径是:

/home/内容/a/d/m/admwta/html/eqflow/

我有一个这样的目录结构:

eqflow
->api
   ->classes
     ->security
     ->utils
   ->v1

我在security或util目录中为每个文件定义了一个类。所有文件都是小写的,对于类名,我遵循了从_到/的PEAR约定,因此安全目录目录中名为getpasswordhash.php的文件名为api_classes_security_getpasswordhash。

我有这个自动加载功能:

function replaceunderscores ($classname) {
$path = str_replace('_', DIRECTORY_SEPARATOR, $classname);
$fullpath = "/home/content/a/d/m/admwta/html/eqflow/".$path.".php";
echo $fullpath . " 'n";
if (file_exists($fullpath))  {
    require_once ($fullpath);
}
else {
    echo "could not find file 'n";
}
}
spl_autoload_register('replaceunderscores');

当我调用login。php时,它总是失败,显示如下消息

/home/内容/a/d/m/admwta/html/eqflow/api/类/安全/getpasswordhash.php找不到文件
致命错误: Class 'api_classes_security_getpasswordhash' not found in /home/content/a/d/m/admwta/html/eqflow/api/v1/login.php on line 27

在自动加载脚本中没有通过file_exists测试,我不知道为什么?你可以看到在echo语句中我输入了echo full path我给出了文件的完整路径?

我通过改变在自动加载函数中构造$fullpath的方式来实现这一点。我没有硬编码路径,而是使用了$_SERVER["DOCUMENT_ROOT"]变量。所以对我来说路径变成了:

$fullpath =  $_SERVER["DOCUMENT_ROOT"]."/eqflow/".$path.".php";

效果很好。我不确定硬编码文档根和使用服务器变量之间的区别是什么,但使用服务器变量工作。