-1
不知道爲什麼這不起作用..嘗試多種不同的語法來調用spl_autoload_register註冊「autoLoad」函數,並且一直收到錯誤,找不到函數。PHP從同一類的構造函數註冊自動加載功能
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>';
}
}
}
}
}
哦,是我不好,沒有意識到我不得不來訪問它的方式...謝謝! – obmon