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有什么用呢?
//自动挂载类
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有什么用呢?