Error Identifier: property.dynamicName
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 User
{
public string $name = '';
public string $email = '';
}
function getValue(User $user, string $field): mixed
{
return $user->$field;
}
Why is it reported? #
The code accesses an object property using a variable name ($object->$variable) instead of a static identifier ($object->name). Variable property access makes the code harder to analyse statically because PHPStan cannot determine which property is being accessed. It also makes the code harder to refactor and more error-prone, since there is no compile-time check that the property exists.
This rule is provided by the phpstan-strict-rules package.
How to fix it #
Replace the variable property access with explicit property access:
<?php declare(strict_types = 1);
-function getValue(User $user, string $field): mixed
+function getValue(User $user, string $field): string
{
- return $user->$field;
+ return match ($field) {
+ 'name' => $user->name,
+ 'email' => $user->email,
+ default => throw new \InvalidArgumentException("Unknown field: $field"),
+ };
}
Alternatively, use a getter method or implement a controlled access pattern that does not rely on dynamic property names.
How to ignore this error #
You can use the identifier property.dynamicName to ignore this error using a comment:
// @phpstan-ignore property.dynamicName
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: property.dynamicName
Rules that report this error #
- PHPStan\Rules\VariableVariables\VariablePropertyFetchRule [1] phpstan/phpstan-strict-rules