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):
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
Properties would need to be declared within a class, your class should look something as shown below for it to work:
More details can be found here:
https://www.php.net/manual/en/language.oop5.visibility.php
Your first snippet should declare the visibility inside the constructor arguments. There is then no need to reference it again in the constructor body