website/errors/property.deprecated.md
<?php declare(strict_types = 1);
class User
{
/**
* @deprecated Use getName() instead
*/
public string $name;
public function getName(): string
{
return $this->name;
}
}
function greet(User $user): string
{
return 'Hello, ' . $user->name; // ERROR: Access to deprecated property $name of class User: Use getName() instead
}
The code accesses a property that has been marked as @deprecated in its PHPDoc. Deprecated properties are scheduled for removal in a future version and should no longer be used. The deprecation notice typically suggests an alternative approach.
This rule is provided by the phpstan/phpstan-deprecation-rules package.
Replace the deprecated property access with the suggested alternative:
<?php declare(strict_types = 1);
function greet(User $user): string
{
- return 'Hello, ' . $user->name;
+ return 'Hello, ' . $user->getName();
}