0

This code produces the error:

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

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

        parent::__construct();
    }

My composer.json uses

"php": ">=7.1.3",

And i use Symfony 4.4 which is a requirement for my job.

I found somewhere on the internet that this type of error is caused by outdated version of php but i am not sure if it's the case in this specific situation. My question is if i can write the code differently or if my only option is to go to a higher php version?

FDjawid
  • 241
  • 3
  • 23
  • 4
    This is a _Type declaration_. https://www.php.net/manual/en/language.types.declarations.php: _“Type declarations can be added to function arguments, return values, **and, as of PHP 7.4.0, class properties**.”_ – CBroe Mar 30 '21 at 11:03
  • _“My question is if i can write the code differently”_ - if this is not _your_ code, but comes from some external component/framework - then don’t even try. Update PHP to the minimum version required by the components you intend to use, everything else would make very little sense. – CBroe Mar 30 '21 at 11:04
  • Thank you for this explanation, it makes sense now for me. – FDjawid Mar 30 '21 at 11:41
  • 1
    Might want to mention this to your boss and see if your team can't update to PHP 7.4 or even 8.0. Sometimes there are issues preventing upgrading but sometimes it's just because no one has thought to do so. – Cerad Mar 30 '21 at 11:51
  • Yes that's a good suggestion, thanks. – FDjawid Mar 30 '21 at 12:11

1 Answers1

1

You need not put

EntityManagerInterface

in the variable declaration.

So, please replace

private EntityManagerInterface $entityManager;

By

private $entityManager;

Its dependency Injection is already added in Constructor function parameters:

public function __construct(EntityManagerInterface $entityManager)

Pupil
  • 23,834
  • 6
  • 44
  • 66