PhpStorm Missing property's type declaration on protected property

Viewed 5828

I have class \App\Console\Kernel which extends from vendor class \Illuminate\Foundation\Console\Kernel. In vendor class there is $commands protected property with no type declaration, so in my own class I could not declare property type.

protected $commands = [
        // commands ...
];

But PhpStorm gives me next error:

Missing property's type declaration

Inspection info: Reports the properties that have no type declaration.

What to do?

1 Answers

As of PHP 7.4 you can declare types on properties. So you could declare your property like this, hard typing it to an array.

protected array $commands = [];

Laravel is backwards compatible to PHP 7.1.3 or 7.2.5 depending on the version of Laravel and therefor does not implement this, so some classes that deal with inheritance you can not do this, as i remember fillable, guarded etc on models. As you describe it, also in your case.

The PHPStorm inspection is a suggestion and is not breaking, it is just trying to make you use PHP 7.4 features, if you are not running 7.4 i would recommend setting PHPStorm to the correct version of PHP.

Related