PHP 8.4 #[\Deprecated] for your Drupal services
Last month I renamed a public method on a service in one of my custom Drupal modules, and left the old name in place as a thin wrapper so nothing downstream would break. Two weeks later I could not remember which wrappers were meant to be temporary. The usual answer is an @deprecated docblock plus a hand-written @trigger_error() call, and I have forgotten the second half more than once. Since PHP 8.4 there is a built-in way to do both at once.
The attribute
The #[\Deprecated] attribute takes two optional arguments, $message and $since. PHP does not validate $since, so a version, a date or anything else is fine. It can target functions, methods, class constants and classes (traits and global constants were added in PHP 8.5). Putting it on a parameter is a compile-time error. When the marked code is used, PHP emits an E_USER_DEPRECATED error whose text includes your message and, if given, the since value.
Here is the service from my module, with the old name kept as a wrapper:
<?php
namespace Drupal\mymodule;
final class PriceFormatter {
#[\Deprecated(message: 'use format() instead', since: '2.1.0')]
public function formatPrice(float $amount): string {
return $this->format($amount, 'EUR');
}
public function format(float $amount, string $currency): string {
return $currency . ' ' . number_format($amount, 2, '.', '');
}
}
Calling formatPrice() still returns the right string, but now PHP also reports something along the lines of "Method Drupal\mymodule\PriceFormatter::formatPrice() is deprecated since 2.1.0, use format() instead". The manual's own example shows the same shape for functions. Note that the notice fires when the method is called, not when the class is loaded, so a dead wrapper nobody calls stays quiet.
Pin it down with a test
A deprecation you cannot see fail is a deprecation you will forget. I like a small test that proves two things: the old API still works, and it announces itself. Since the notice is an E_USER_DEPRECATED error, set_error_handler() can capture it, and returning TRUE from the handler tells PHP the error was handled.
<?php
namespace Drupal\Tests\mymodule\Unit;
use Drupal\mymodule\PriceFormatter;
use PHPUnit\Framework\TestCase;
final class PriceFormatterTest extends TestCase {
public function testOldMethodStillWorksAndWarns(): void {
$messages = [];
set_error_handler(static function (int $errno, string $errstr) use (&$messages): bool {
$messages[] = $errstr;
return TRUE;
}, E_USER_DEPRECATED);
try {
$result = (new PriceFormatter())->formatPrice(9.5);
}
finally {
restore_error_handler();
}
$this->assertSame('EUR 9.50', $result);
$this->assertCount(1, $messages);
$this->assertStringContainsString('use format() instead', $messages[0]);
}
}
Do not skip the finally block. If the assertion code throws before restore_error_handler() runs, your handler leaks into every later test in the process. Once you delete the wrapper in the next major version, this test goes with it, which is a decent reminder that the removal is on the calendar.
Finding deprecated code with reflection
The attribute is also machine-readable. ReflectionFunctionAbstract::isDeprecated() and ReflectionClassConstant::isDeprecated() exist for this, so a script can sweep your module and list everything scheduled for removal:
<?php
$method = new \ReflectionMethod(\Drupal\mymodule\PriceFormatter::class, 'formatPrice');
var_dump($method->isDeprecated());
I use that idea to print a removal checklist before tagging a release. I have not wired it into CI yet, so treat that part as a sketch.
As for the gotchas: Drupal 11 still runs on PHP 8.3, where an unknown attribute is simply ignored, so on 8.3 you get no runtime notice and the attribute is only metadata. For code that has to behave the same everywhere, Drupal core's established pattern of a @deprecated docblock plus @trigger_error() is still the safe choice. For a private module where you control the PHP version, though, the attribute is shorter, harder to forget, and it keeps the replacement message right next to the signature it describes.