Error Identifier: property.readOnlyInInterface
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 readonly string $name { get; } // ERROR: Interfaces cannot include readonly hooked properties.
}
Why is it reported? #
PHP 8.4 allows interfaces to declare hooked properties, but the readonly modifier is not permitted on interface properties. The readonly modifier is a concrete implementation detail that should be decided by the implementing class, not enforced by the interface.
This error is not ignorable because it represents a PHP language-level constraint.
How to fix it #
Remove the readonly modifier from the interface property declaration:
<?php declare(strict_types = 1);
interface HasName
{
- public readonly string $name { get; }
+ public string $name { get; }
}
The implementing class can still declare the property as readonly if desired:
<?php declare(strict_types = 1);
class User implements HasName
{
public function __construct(
public readonly string $name,
) {}
}
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]