Error Identifier: property.nonStatic
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);
class Base
{
public static string $name = 'base';
}
class Child extends Base
{
public string $name = 'child';
}
Why is it reported? #
A non-static property in a child class overrides a static property in the parent class. PHP does not allow changing the static/non-static nature of a property during inheritance. This causes a fatal error at runtime.
How to fix it #
Match the parent property’s static modifier:
class Child extends Base
{
- public string $name = 'child';
+ public static string $name = 'child';
}
Or redesign the class hierarchy so the property does not conflict:
class Child extends Base
{
- public string $name = 'child';
+ public string $childName = 'child';
}
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\OverridingPropertyRule [1]