Error Identifier: property.inInterface
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
interface HasName
{
public string $name; // ERROR: Interfaces can include properties only on PHP 8.4 and later.
}
Why is it reported? #
Before PHP 8.4, interfaces cannot declare properties. Property declarations in interfaces are a PHP 8.4 feature that works together with property hooks, allowing interfaces to define abstract hooked properties that implementing classes must provide.
This error is not ignorable because it represents a PHP language-level constraint.
How to fix it #
If running PHP 8.4 or later, declare the property as a hooked property in the interface:
<?php declare(strict_types = 1);
interface HasName
{
- public string $name;
+ public string $name { get; set; }
}
On earlier PHP versions, use getter and setter methods instead:
<?php declare(strict_types = 1);
interface HasName
{
- public string $name;
+ public function getName(): string;
+ public function setName(string $name): void;
}
Non-ignorable error #
This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.
Rules that report this error #
- PHPStan\Rules\Properties\PropertiesInInterfaceRule [1]