不知道为什么这不起作用..尝试了许多不同的语法来调用注册“autoLoad”函数与 spl_autoload_register,我不断收到错误,找不到函数。
class ClassLoader {
//Directories
private $dir_path = '';
private $directories = ['config/',
'core/',
'helpers/',
'modules/',
'classes/'];
//Add your file naming formats here
private $fileNameFormats = ['%s.php',
'%s.class.php',
'class.%s.php',
'%s.inc.php'];
public function __construct($paths) {
$this->dir_path = $paths['root']. '/';
$loader = $this->{autoLoader()};
spl_autoload_register($loader);
}
function autoLoader($className) {
foreach($this->directories as $directory) {
foreach($this->fileNameFormats as $fileNameFormat) {
$path = $this->dir_path . $directory.sprintf($fileNameFormat, $className);
try {
if (!include($path)) {
throw new Exception ('<b>Error - Missing class:</b>' . $path);
}
}
catch (Exception $e) {
echo
'<p><b>EXCEPTION</b><br />Message: '
. $e->getMessage()
. '<br />File: '
. $e->getFile()
. '<br />Line: '
. $e->getLine()
. '</p>';
}
}
}
}
}