-1

不知道为什么这不起作用..尝试了许多不同的语法来调用注册“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>';
            }
        }
    }
}
}
4

1 回答 1

4

根据有关回调的文档,要将类函数引用为回调,您需要一个数组,其中第一个元素是类名或表示类实例的对象,以及您想要的函数字符串那个班级的电话。

所以,你应该使用:

spl_autoload_register( array( $this, 'autoLoader'));
于 2013-01-13T00:18:31.737 回答