-1

これがうまくいかない理由がわからない.. spl_autoload_register を使用して「autoLoad」関数を登録するためにさまざまな構文を試してみると、関数が見つからないというエラーが発生し続けます。

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 に答える