Error Identifier: property.hookBodyInInterface
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 $firstName {
get {
return 'Foo';
}
}
}
Why is it reported? #
Interface property hooks cannot have bodies. Properties declared in interfaces are implicitly abstract, meaning their hooks must be declared without a body. The implementing class is responsible for providing the hook implementation.
This is a PHP language constraint introduced with property hooks in PHP 8.4.
How to fix it #
Remove the hook body and declare only the hook signature:
<?php declare(strict_types = 1);
interface HasName
{
- public string $firstName {
- get {
- return 'Foo';
- }
- }
+ public string $firstName { get; }
}
Then provide the implementation in a class:
<?php declare(strict_types = 1);
class User implements HasName
{
public string $firstName {
get {
return 'Foo';
}
}
}
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]