Home > OS >  PhpStorm is set to 8.1.7 but throws error on 8.0.* methods
PhpStorm is set to 8.1.7 but throws error on 8.0.* methods

Time:07-13

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;
}

CodePudding user response:

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) {}

CodePudding user response:

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

  • Related