website/errors/staticProperty.dynamicName.md
This error is reported by phpstan/phpstan-strict-rules.
<?php declare(strict_types = 1);
class Registry
{
public static string $name = 'default';
public static function get(string $property): string
{
return Registry::$$property;
}
}
Accessing a static property with a dynamic (variable) name makes the code harder to follow and analyse statically. PHPStan cannot verify that the variable holds a valid property name, which may lead to runtime errors that go undetected during analysis.
Replace the variable static property access with explicit property references.
<?php declare(strict_types = 1);
class Registry
{
public static string $name = 'default';
- public static function get(string $property): string
+ public static function get(string $property): ?string
{
- return Registry::$$property;
+ return match ($property) {
+ 'name' => Registry::$name,
+ default => null,
+ };
}
}