身份不明的php语法错误

时间:2014-04-04 15:32:46

标签: php syntax-error

我正在尝试使用预先编写的PHP自动加载器,而Dreamweaver则声明存在语法错误。有人可以解释导致问题的原因。

显示有语法错误的行是 $ autoloader = new static($ dir,$ ext); spl_autoload_register([$ autoloader,' load']);

<?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 '';
    }
}

1 个答案:

答案 0 :(得分:4)

spl_autoload_register([$autoloader, 'load']);

此数组语法仅在PHP 5.4中有效。如果DreamWeaver使用旧版本的PHP语法规则,它会抱怨。