Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.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_Oop_Autoloader - Fatal编程技术网

php运行时错误

php运行时错误,php,oop,autoloader,Php,Oop,Autoloader,我有一个PHP脚本,它产生以下错误 分析错误:语法错误,第20行的/home/masked/public_html/masked/AutoLoader.php中出现意外的“[”,应为“') (请注意,我屏蔽了给定目录的某些部分,因为它们与个人信息相对应。) (第20行是spl_autoloader_寄存器([$autoloader,'load']);位于寄存器函数中 我正在运行PHP5.5 我还用PHP5.3.27进行了尝试,得到了完全相同的结果 暗号 run.php require_once

我有一个PHP脚本,它产生以下错误

分析错误:语法错误,第20行的/home/masked/public_html/masked/AutoLoader.php中出现意外的“[”,应为“')

(请注意,我屏蔽了给定目录的某些部分,因为它们与个人信息相对应。)

(第20行是spl_autoloader_寄存器([$autoloader,'load']);位于寄存器函数中

我正在运行PHP5.5 我还用PHP5.3.27进行了尝试,得到了完全相同的结果

暗号

run.php

require_once 'AutoLoader.php';
AutoLoader::register('src');
AutoLoader.php

/**
 * A basic PSR style autoloader
 */
class AutoLoader
{
    protected $dir;
    protected $ext;

    public function __construct($dir, $ext = '.php')
    {
        $this->dir = rtrim($dir, DIRECTORY_SEPARATOR);
        $this->ext = $ext;
    }

    public static function register($dir, $ext = '.php')
    {
        $autoloader = new static($dir, $ext);
        spl_autoload_register([$autoloader, 'load']);

        return $autoloader;
    }

    public function load($class)
    {
        $dir = $this->dir;

        if ($ns = $this->get_namespace($class)) {
            $dir .= DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $ns);
        }

        $inc_file = $dir.DIRECTORY_SEPARATOR.$this->get_class($class).$this->ext;

        if (file_exists($inc_file)) {
            require_once $inc_file;
        }
    }

    // Borrowed from github.com/borisguery/Inflexible
    protected static function get_class($value)
    {
        $className = trim($value, '\\');

        if ($lastSeparator = strrpos($className, '\\')) {
            $className = substr($className, 1 + $lastSeparator);
        }

        return $className;
    }

    // Borrowed from github.com/borisguery/Inflexible
    public static function get_namespace($fqcn)
    {
        if ($lastSeparator = strrpos($fqcn, '\\')) {
            return trim(substr($fqcn, 0, $lastSeparator + 1), '\\');
        }

        return '';
    }

}
在run.php上尝试:

require_once('Autoloader.php');

您正在运行哪个版本的PHP?请仔细检查。使用
[]
语法定义数组(根据
spl\u autoload\u寄存器([$autoloader,'load']);
)需要PHP>=5.4可以正常工作,尽管它显然与使用两个文件有点不同。但是,您确定这段代码中没有其他内容吗?@samcopock这听起来可能很奇怪,但是-您可以添加
echo phpversion()吗
的最开始运行.php
文件来检查这是否是PHP5.5?从另一个角度来看:当您将
[$autoloader,'load']
替换为
数组($autoloader,'load')
时会发生什么?此外,这是我们在这里关于Stackoverflow的php错误参考中包含的常见错误: