我正在实现一个自动加载器类,但它不工作。下面是自动加载器类(灵感来自php.net 上的这个页面):
class System
{
public static $loader;
public static function init()
{
if (self::$loader == NULL)
{
self::$loader = new self();
}
return self::$loader;
}
public function __construct()
{
spl_autoload_register(array($this, "autoload"));
}
public function autoload($_class)
{
set_include_path(__DIR__ . "/");
spl_autoload_extensions(".class.php");
spl_autoload($_class);
print get_include_path() . "<br>\n";
print spl_autoload_extensions() . "<br>\n";
print $_class . "<br>\n";
}
}
调用自动加载器的代码在这里:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
require_once __DIR__ . "/system/System.class.php";
System::init();
$var = new MyClass(); // line 9
print_r($var);
?>
和错误信息:
/home/scott/www/system/
.class.php
MyClass
Fatal error: Class 'MyClass' not found in /home/scott/www/index.php on line 9
正在命中自动加载功能,文件 MyClass.class.php 存在于包含路径中,我可以通过将代码更改为以下内容来验证:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
require_once __DIR__ . "/system/System.class.php";
require_once __DIR__ . "/system/MyClass.class.php";
System::init();
$var = new MyClass();
print_r($var);
?>
print_r($var);
返回对象并且没有错误。
有什么建议或指示吗?