基本上,我在myclass
中有php类root/lib/php/
,我希望每当我调用它们时,类都会自动加载。
现在我尝试在主index.php文件中执行此操作。我应该如何以及在何处正确定义spl_autoload_register
,以便将其用于静态类:myclass::mymethod()
?
答案 0 :(得分:1)
通常自动加载编码包含在配置文件或每个页面加载中包含的其他文件中,并包含网站运行所需的公共代码。
对类方法的静态调用与实例化一个类并就自动加载一样调用方法没有什么不同。
一个例子:
/*** nullify any existing autoloads ***/
spl_autoload_register(null, false);
/*** specify extensions that may be loaded ***/
spl_autoload_extensions('.Class.php');
/*** class Loader ***/
define('BASE_APP_PATH', '/path/to/root/web/');
function classLoader($class){
$filename = $class . '.Class.php';
$file = BASE_APP_PATH.'classes/' . $filename;
if (file_exists($file)){
include $file;
}
}
/*** register the loader functions ***/
spl_autoload_register('classLoader');
然后,您可以在不明确要求类'file:
的情况下执行此操作echo SomeClass::staticCall();