Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php spl#U自动加载#U寄存器';b/n在线和本地主机的不同行为_Php_Closures_Spl_Spl Autoload Register_Spl Autoloader - Fatal编程技术网

Php spl#U自动加载#U寄存器';b/n在线和本地主机的不同行为

Php spl#U自动加载#U寄存器';b/n在线和本地主机的不同行为,php,closures,spl,spl-autoload-register,spl-autoloader,Php,Closures,Spl,Spl Autoload Register,Spl Autoloader,我有下面的目录结构,我把我所有的类放在classes/中,如下所示,我有一个header.php,它调用所有文件,并有所有文件共享的其他重要设置 现在,如果我这样使用spl\u autoload\u register(): spl_autoload_register(function($class){ include 'classes/'. $class .'.class.php'; }); 在inc/header.php文件中,从我的index文件调用这个header


我有下面的目录结构,我把我所有的类放在
classes/
中,如下所示,我有一个
header.php
,它调用所有文件,并有所有文件共享的其他重要设置

现在,如果我这样使用
spl\u autoload\u register()

spl_autoload_register(function($class){
        include 'classes/'. $class .'.class.php';
    });
inc/header.php
文件中,从我的
index
文件调用这个
header.php
,然后它在本地主机上运行良好,但是当我将所有脚本按原样上传到一个实时主机时,就会出现如下错误

Warning: include(classes/filehandler.class.php) [function.include]: failed to open stream: No such file or directory in /home/.../public_html/....com/inc/header.php on line 9 
第9行是
spl\u自动加载寄存器()

我不明白,这是怎么发生的,希望任何人都知道


感谢

要说明我的评论的意思,请在index.php文件中添加此代码,并从其他文件中删除自动加载器:

spl_autoload_register(function($class){
    $classesPath = dirname(__FILE__) . '/classes/';
    if (is_file($classFile = $classesPath . $class.'.class.php')) {
        include $classFile;
    }
});
对于inc/header.php:

spl_autoload_register(function($class){
    $classesPath = dirname(__FILE__) . '/../classes/';
    if (is_file($classFile = $classesPath . $class.'.class.php')) {
        include $classFile;
    }
});

“没有这样的文件或…”后看到的路径正确吗?它指向你的头文件吗?@ilpaijin是的,它指向正确的目录。这就是让人困惑的地方。加载类时,应该使用绝对路径,而不是相对路径。此外,您不仅要将
包含在自动加载程序中,还要检查类文件是否确实存在于您的类文件夹中(同样,绝对路径)。L.E:另一个注意事项,在您当前的示例中,自动加载程序在
inc
文件夹中查找
classes
文件夹,这是因为您使用相对路径,所以再次使用绝对路径(
\uuu FILE\uuuuuuuuuuu
\uuuuuu DIR\uuuuuuuuuuu
应该可以帮助您)。尝试调用get\u include\u path(),看看有没有什么wrong@Twisted1919绝对是怎么可能的呢?您的意思是从
c://
文件夹引用它,还是通过
http://
引用它?顺便说一句:我在这里使用它作为正确用法的例子?@twisted1919是正确的。始终检查文件是否作为公共文件存在practice@Twisted1919我想尝试一下,但是除了
index.php
文件之外,我的根目录中还有13个以上的文件。因此,您的方法要求我将其添加到所有这些文件中。因此,唯一方便的方法是将其添加到
inc/header.php
中,并在所有REST中仅调用此文件似乎不起作用。我正在尝试不同的方法组合来查找文件,但仍然得到相同的结果error@Xlaltra-如果您
echo realpath($classesPath)
在你的自动加载器中,它说了什么?我得到了
/home/example/public\u html/mysite.com/classes
,这似乎是我试图在@Twisted1919中加载类的目录