skip to Main Content

I have this code snippet that PhpStorm does not like:

public function __construct(LogFacade $logFacade)
{
    private LogFacade $logFacade
} ()

It mainly says Undefined constant 'LogFacade' and Expected: semicolon.

We are running on docker container which has PHP version 8.1.7:

martin@463a39853ae1:/web$ php -v
PHP 8.1.7 (cli) (built: Jun 13 2022 13:56:32) (ZTS)
Copyright (c) The PHP Group
Zend Engine v4.1.7, Copyright (c) Zend Technologies
martin@463a39853ae1:/web$

and so is PhpStorm (language level+interpreter):
PhpStorm version of PHP

When I change it to older version, it works completely fine (But the project is using the first snippet all over the place:

private LogFacade $logFacade;

public function __construct(LogFacade $logFacade)
{
    $this->logFacade = $logFacade;
}

2

Answers


  1. Properties would need to be declared within a class, your class should look something as shown below for it to work:

    class MyClass
    {
        private LogFacade $logFacade;
    
        public function __construct(LogFacade $logFacade)
        {
            $this->logFacade = $logFacade;
        }
    }
    

    More details can be found here:
    https://www.php.net/manual/en/language.oop5.visibility.php

    Login or Signup to reply.
  2. Your first snippet should declare the visibility inside the constructor arguments. There is then no need to reference it again in the constructor body

    public function __construct(private LogFacade $logFacade) {}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search