您应该命名您的类,以便将下划线(
_)转换为目录分隔符(
/)。一些PHP框架可以做到这一点,例如Zend和Kohana。
因此,您可以为课程命名,然后
Model_Article将文件放入其中
classes/model/article.php,然后自动加载就可以了…
function __autoload($class_name) { $filename = str_replace('_', DIRECTORY_SEPARATOR, strtolower($class_name)).'.php'; $file = AP_SITE.$filename; if ( ! file_exists($file)) { return FALSE; } include $file;}还要注意,您可以
spl_autoload_register()使任何功能成为自动加载功能。它也更加灵活,允许您定义多个自动加载类型的函数。
如果必须有多个自动加载功能,则spl_autoload_register()允许这样做。它有效地创建了自动加载功能队列,并按照定义的顺序遍历每个功能。相反,__
autoload()只能定义一次。
编辑
注意: 自PHP 7.2.0起,__ autoload
已被弃用。强烈建议不要使用此功能。请参阅PHP文档以获取更多详细信息。http://php.net/manual/zh/function.autoload.php



