spl_autoload_register 在在线和本地主机上的不同行为
-
21-12-2019 - |
题
我有以下目录结构,我把所有的类都放在里面 classes/
如下所示,我有一个 header.php
它既调用所有文件,又具有所有文件共享的其他重要设置。
现在,如果我使用 spl_autoload_register()
这边走:
spl_autoload_register(function($class){
include 'classes/'. $class .'.class.php';
});
里面 inc/header.php
文件并调用它 header.php
从我的 index
文件,然后它在我的本地主机上工作正常,但是当我将所有脚本按原样上传到实时主机时,我会收到类似的错误。
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
Line 9
是个 spl_autoload_register()
我不明白,这是怎么发生的,希望有人有任何想法。
谢谢
解决方案
为了展示我的评论的意思,请在您的 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;
}
});
不隶属于 StackOverflow