ParseError:语法错误,意外的“EntityManagerInterface”T_STRING,需要函数T_FUNCTION或常量T_CONST

问题描述

此代码产生错误:

     /**
     * @var EntityManagerInterface
     */
    private EntityManagerInterface $entityManager;

    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;

        parent::__construct();
    }

我的 composer.json 使用

"php": ">=7.1.3",

我使用 Symfony 4.4,这是我工作的要求。

我在互联网上的某个地方发现这种类型的错误是由过时的 php 版本引起的,但我不确定在这种特定情况下是否是这种情况。我的问题是我是否可以编写不同的代码,或者我唯一的选择是使用更高的 php 版本?

解决方法

你不用放

EntityManagerInterface

在变量声明中。

所以,请更换

private EntityManagerInterface $entityManager;

通过

private $entityManager;

它的依赖注入已经添加在Constructor函数参数中:

public function __construct(EntityManagerInterface $entityManager)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...