簡體   English   中英

PHP運行時錯誤

[英]A php runtime error

我有一個PHP腳本正在產生以下錯誤

解析錯誤:語法錯誤,在第20行的/home/masked/public_html/masked/AutoLoader.php中出現意外的[[,期待')'

(請注意,我屏蔽了目錄中與個人信息相對應的某些部分。)

(第20行是spl_autoload_register([$ autoloader,'load']);在寄存器函數中找到。

我正在運行PHP 5.5,我也嘗試使用PHP 5.3.27並獲得完全相同的結果。

波紋管

運行.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');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM