#[\Deprecated] Attribute
PHP 8.4 lets you mark your own functions, methods, and class constants as deprecated using a native attribute — the same mechanism PHP itself uses internally. When someone calls deprecated code, they get a proper E_USER_DEPRECATED notice with your custom message.
class PaymentService {
// Deprecate with a message and version since
#[\Deprecated("Use processPayment() instead", since: "3.2")]
public function charge(float $amount): bool {
return $this->processPayment($amount);
}
public function processPayment(float $amount): bool {
// New implementation
}
}
$service = new PaymentService();
$service->charge(50.00);
// Deprecated: Method PaymentService::charge() is deprecated since 3.2,
// use processPayment() instead
// Works on functions too
#[\Deprecated("Use generateUuid() instead")]
function createId(): string {
return generateUuid();
}
// And class constants
class Config {
#[\Deprecated("Use TIMEOUT_SECONDS instead")]
const TIMEOUT = 30;
const int TIMEOUT_SECONDS = 30;
}
Before this attribute, library authors had to manually trigger trigger_error() inside deprecated methods — cluttering the implementation and offering no standard format. Now deprecation is metadata, clean and consistent.
Significance: API Evolution
Every library needs to evolve its API. The
#[\Deprecated] attribute gives library authors a standard, engine-recognized way to guide users toward new APIs — without breaking backward compatibility or cluttering method bodies with trigger_error() calls.