我开始使用PHP的__autoload函数,现在我遇到了一些奇怪的致命错误:无法重新声明类xxx。
这很奇怪,因为这些错误发生在我甚至没有使用autoload函数加载的类上。而且我还使用require_once来包含这些文件。
我对此非常困惑。有人知道在使用autoload时出现这种错误的原因吗?
我开始使用PHP的__autoload函数,现在我遇到了一些奇怪的致命错误:无法重新声明类xxx。
这很奇怪,因为这些错误发生在我甚至没有使用autoload函数加载的类上。而且我还使用require_once来包含这些文件。
我对此非常困惑。有人知道在使用autoload时出现这种错误的原因吗?
require_once
/include_once
只在尝试包含文件时查看文件名,而不是类名。因此,你可以在Foo.php和B.php中都有类Foo
,然后你会得到错误信息。__autoload
会给你带来什么问题,除非__autoload
需要Foo.php,因为它需要类Foo
,而你手动需要了B.php,重新定义了类Foo
。spl_autoload_register
代替__autoload
。我曾经遇到同样的情况...我所做的仅仅是更改了 and(某种代码或参数),现在它能够正常工作了。
include('xxx.php');
为了
require_once('xxx.php');
//initialize a function which will upload files
function upload(){//you can call the function as you like
//your path to files goes here
}
spl_autoload_register('upload')//specify the function, where you include your files(in this case it's upload function
require_once
/include_once
而不是普通版本。我认为问题与使用class_exists
有关,而没有告诉它跳过自动加载(class_exists($name, false)
)- 你是否这样做了?