website/errors/method.deprecated.md
This error is reported by the phpstan-deprecation-rules extension.
<?php declare(strict_types = 1);
class UserRepository
{
/** @deprecated Use findById() instead */
public function getUser(int $id): object
{
return $this->findById($id);
}
public function findById(int $id): object
{
// ...
}
}
$repo = new UserRepository();
$repo->getUser(1); // ERROR: Call to deprecated method getUser() of class UserRepository.
A method marked with the @deprecated PHPDoc tag is being called. Deprecated methods are scheduled for removal in a future version and should no longer be used. The deprecation notice typically provides guidance on what to use instead.
Replace the call with the recommended alternative:
<?php declare(strict_types = 1);
$repo = new UserRepository();
-$repo->getUser(1);
+$repo->findById(1);
If a child class overrides a deprecated method from its parent and the override is no longer deprecated, use @not-deprecated to break the inheritance chain:
class ChildRepository extends UserRepository
{
+ /** @not-deprecated */
public function getUser(int $id): object
{
// new implementation
}
}
$child = new ChildRepository();
$child->getUser(1); // OK
Without @not-deprecated, the overridden method inherits the @deprecated tag from the parent class and calls to it are still reported as deprecated. Learn more in PHPDoc Basics.