对php spl_autoload_register的疑问

2013-08-03 08:25:35 +08:00
 zencoding
init.inc.php的内容

//自动挂载类
function AutoLoad($classname){
// class类
$filepath = BASE_CLASS . $classname . '.class.php';
if (file_exists($filepath)) {
return include $filepath;
}
//lib库文件
$filepath = BASE_LIB . $classname . '.lib.php';
if (file_exists($filepath)) {
return include $filepath;
}
}
spl_autoload_register('AutoLoad');

在建一个test.class.php放在class文件夹下
class test{
function method(){
echo 'Hello world';
}
}


然后index.php

require 'init.inc.php';
AutoLoad('test');
$test = new testclass();
$test->method();

如果在init.inc.php中注释掉spl_autoload_register('AutoLoad');
一样成功,问题就来了,spl_autoload_register有什么用呢?
2634 次点击
所在节点    问与答
3 条回复
donwa
2013-08-03 08:32:30 +08:00
不用autoload('test');
找不到类它会自己执行autoload
zencoding
2013-08-03 08:39:19 +08:00
@donwa 前提是类文件名和类名要一致
xiaokai
2013-08-03 10:58:22 +08:00
这不应该成功嘛 $test = new testclass(); ?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/77834

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX