Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP从同一类中的构造函数中注册自动加载函数_Php_Autoload - Fatal编程技术网

PHP从同一类中的构造函数中注册自动加载函数

PHP从同一类中的构造函数中注册自动加载函数,php,autoload,Php,Autoload,不知道为什么这不起作用。。尝试使用多种不同的语法调用register和spl_autoLoad_register的“autoLoad”函数,我不断得到一个错误,function not found class ClassLoader { //Directories private $dir_path = ''; private $directories = ['config/', 'core/',

不知道为什么这不起作用。。尝试使用多种不同的语法调用register和spl_autoLoad_register的“autoLoad”函数,我不断得到一个错误,function not found

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>';
            }
        }
    }
}
}
类加载器{
//目录
私有$dir_path='';
私有$directories=['config/',
'核心/',
“助手/”,
“模块/”,
“类/”];
//在此处添加文件命名格式
私有$fileNameFormats=['%s.php',
“%s.class.php”,
'class.%s.php',
“%s.inc.php”];
公共函数构造($path){
$this->dir_path=$path['root']./;
$loader=$this->{autoLoader()};
spl_自动加载_寄存器($loader);
}
函数自动加载程序($className){
foreach($this->directory as$directory){
foreach($this->fileNameFormats为$fileNameFormat){
$path=$this->dir\u path.$directory.sprintf($fileNameFormat,$className);
试一试{
如果(!包含($path)){
引发新异常('错误-缺少类:'。$path);
}
}
捕获(例外$e){
回响
“异常
消息:” .$e->getMessage() “
文件:” .$e->getFile() “
行:” .$e->getLine() “

”; } } } } }
根据,要引用类函数作为回调,需要一个数组,其中第一个元素是类名或表示类实例的对象,以及要从该类调用的函数字符串

因此,您应该使用:

spl_autoload_register( array( $this, 'autoLoader'));

哦,我的错,我没意识到我必须用那种方式访问它。。。谢谢