【问题标题】:Symfony 2.8 Parse error: syntax error, unexpected ':', expecting ';' or '{' in c:\xampp\htdocs\myproject\path\to\AnnotationRegistry.php on line 50Symfony 2.8 解析错误:语法错误,意外 ':',期待 ';'或 '{' 在 c:\xampp\htdocs\myproject\path\to\AnnotationRegistry.php 第 50 行
【发布时间】:2018-01-08 01:09:08
【问题描述】:

我已经从我自己的 PC 上下载并安装了 Symfony 2.8。我复制了我的项目并将其粘贴到我们公司的电脑中。我还没有创建并连接到数据库。我尝试在c:\xampp\htdocs\ninjaz\ 上执行php app/console server:run,但之后我收到此错误消息。

Parse error: syntax error, unexpected ':', expecting ';' or '{' in C:\xampp\htdo cs\Ninjaz\vendor\doctrine\annotations\lib\Doctrine\Common\Annotations\Annotation Registry.php on line 50

编辑: 我自己的 PC 安装了带有 PHP 7.1 的 XAMPP。我们公司的PC有XAMPP PHP 5.5.19

这是AnnotationRegistry.php的内容:

<?php

namespace Doctrine\Common\Annotations;

final class AnnotationRegistry
{
/**
 * A map of namespaces to use for autoloading purposes based on a PSR-0 convention.
 *
 * Contains the namespace as key and an array of directories as value. If the value is NULL
 * the include path is used for checking for the corresponding file.
 *
 * This autoloading mechanism does not utilize the PHP autoloading but implements autoloading on its own.
 *
 * @var string[][]|string[]|null[]
 */
static private $autoloadNamespaces = [];

/**
 * A map of autoloader callables.
 *
 * @var callable[]
 */
static private $loaders = [];

/**
 * An array of classes which cannot be found
 *
 * @var null[] indexed by class name
 */
static private $failedToAutoload = [];

public static function reset() : void
{
    self::$autoloadNamespaces = [];
    self::$loaders            = [];
    self::$failedToAutoload   = [];
}

/**
 * Registers file.
 *
 * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
 *             autoloading should be deferred to the globally registered autoloader by then. For now,
 *             use @example AnnotationRegistry::registerLoader('class_exists')
 */
public static function registerFile(string $file) : void
{
    require_once $file;
}

/**
 * Adds a namespace with one or many directories to look for files or null for the include path.
 *
 * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
 *
 * @param string            $namespace
 * @param string|array|null $dirs
 *
 * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
 *             autoloading should be deferred to the globally registered autoloader by then. For now,
 *             use @example AnnotationRegistry::registerLoader('class_exists')
 */
public static function registerAutoloadNamespace(string $namespace, $dirs = null) : void
{
    self::$autoloadNamespaces[$namespace] = $dirs;
}

/**
 * Registers multiple namespaces.
 *
 * Loading of this namespaces will be done with a PSR-0 namespace loading algorithm.
 *
 * @param string[][]|string[]|null[] $namespaces indexed by namespace name
 *
 * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
 *             autoloading should be deferred to the globally registered autoloader by then. For now,
 *             use @example AnnotationRegistry::registerLoader('class_exists')
 */
public static function registerAutoloadNamespaces(array $namespaces) : void
{
    self::$autoloadNamespaces = \array_merge(self::$autoloadNamespaces, $namespaces);
}

/**
 * Registers an autoloading callable for annotations, much like spl_autoload_register().
 *
 * NOTE: These class loaders HAVE to be silent when a class was not found!
 * IMPORTANT: Loaders have to return true if they loaded a class that could contain the searched annotation class.
 *
 * @deprecated this method is deprecated and will be removed in doctrine/annotations 2.0
 *             autoloading should be deferred to the globally registered autoloader by then. For now,
 *             use @example AnnotationRegistry::registerLoader('class_exists')
 */
public static function registerLoader(callable $callable) : void
{
    // Reset our static cache now that we have a new loader to work with
    self::$failedToAutoload   = [];
    self::$loaders[]          = $callable;
}

/**
 * Autoloads an annotation class silently.
 */
public static function loadAnnotationClass(string $class) : bool
{
    if (\class_exists($class, false)) {
        return true;
    }

    if (\array_key_exists($class, self::$failedToAutoload)) {
        return false;
    }

    foreach (self::$autoloadNamespaces AS $namespace => $dirs) {
        if (\strpos($class, $namespace) === 0) {
            $file = \str_replace('\\', \DIRECTORY_SEPARATOR, $class) . '.php';

            if ($dirs === null) {
                if ($path = stream_resolve_include_path($file)) {
                    require $path;
                    return true;
                }
            } else {
                foreach((array) $dirs AS $dir) {
                    if (is_file($dir . \DIRECTORY_SEPARATOR . $file)) {
                        require $dir . \DIRECTORY_SEPARATOR . $file;
                        return true;
                    }
                }
            }
        }
    }

    foreach (self::$loaders AS $loader) {
        if ($loader($class) === true) {
            return true;
        }
    }

    self::$failedToAutoload[$class] = null;

    return false;
   }
}

我已经设法通过删除那些: void: bool 使服务器运行,但是当我浏览我的项目时,它返回另一个错误,它与第一个相同,但是当我再次尝试删除它时,它在其他库上创建另一个错误。这就像一个错误场景之后的无尽错误。 我不知道现在该怎么办。我只是 Symfony 的新手。 急需帮助。

【问题讨论】:

    标签: php php-5.5 symfony-2.8 php-7.1


    【解决方案1】:

    这是由于 PHP 版本错误造成的。我遇到过同样的问题。你需要 PHP 7+。低于 7 的 PHP 版本不支持“返回类型声明”。

    您也可以尝试将以下代码添加到您的composer.json:

    {
        "config": {
            "platform": {"php": "5.6"}
        }
    }
    

    【讨论】:

    • 是的。我认为这就是原因。我从我的电脑上复制了我的项目,在我们公司里有 PHP 7,我们只有 PHP 5.6。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 2012-05-27
    • 1970-01-01
    • 2016-12-28
    • 2010-12-20
    相关资源
    最近更新 更多