我正在使用spl_autoload_register()
函数来包含所有文件。我希望任何具有扩展名.class.php
或.php
将直接包含的类。我做了下面的课程并注册了两个不同的功能,一切正常,但是
我认为有一些方法可以让我只需要注册一个函数就可以将两个扩展包含在一起。
请查看我的功能并告诉我我缺少什么
我的文件夹结构
project
-classes
-alpha.class.php
-beta.class.php
-otherclass.php
-includes
- autoload.php
-config.inc.php // define CLASS_DIR and include 'autoload.php'
自动加载.php
var_dump(__DIR__); // 'D:\xampp\htdocs\myproject\includes'
var_dump(CLASS_DIR); // 'D:/xampp/htdocs/myproject/classes/'
spl_autoload_register(null, false);
spl_autoload_extensions(".php, .class.php"); // no use for now
/*** class Loader ***/
class AL
{
public static function autoload($class)
{
$filename = strtolower($class) . '.php';
$filepath = CLASS_DIR.$filename;
if(is_readable($filepath)){
include_once $filepath;
}
// else {
// trigger_error("The class file was not found!", E_USER_ERROR);
// }
}
public static function classLoader($class)
{
$filename = strtolower($class) . '.class.php';
$filepath = CLASS_DIR . $filename;
if(is_readable($filepath)){
include_once $filepath;
}
}
}
spl_autoload_register('AL::autoload');
spl_autoload_register('AL::classLoader');
注意:对线没有影响spl_autoload_extensions();
。为什么?
我也读了这个博客,但不明白如何实现。