Skip to main content
Blue Moose logo Blue Moose Field notes
← All articles
code

PHP #[\Override]: catch silent Drupal typos

Lines of code on a dark monitor

Last month I bumped a contrib module and one of my custom blocks quietly stopped invalidating its cache. No error, no log line, nothing. The cause: I had typed getCacheTag() instead of getCacheTags() months earlier. PHP never complained because a method that overrides nothing is just a new method that nobody calls. PHP 8.3's #[\Override] attribute turns that silent failure into a compile-time error, and it costs one line per method.

What the attribute actually does

According to the PHP manual, #[\Override] marks a method as intended to override a parent method or implement an interface method. If no matching method exists in a parent class or an implemented interface, PHP emits a compile-time error. The manual's own example produces Extended::boo() has #[\Override] attribute, but no matching parent method exists. Two details matter: the attribute cannot be used on __construct(), and it was method-only in 8.3. The manual's changelog says 8.5 extends it to properties and 8.6 to class constants, including enum cases.

A Drupal block that cannot silently drift

Drupal plugins are a perfect fit because they are full of methods that only exist by naming convention with the parent. Here is a block that shows a "latest news" list and varies its cache by tag and user role:

<?php

declare(strict_types=1);

namespace Drupal\my_module\Plugin\Block;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

#[Block(
  id: 'my_module_latest_news',
  admin_label: new TranslatableMarkup('Latest news'),
)]
final class LatestNewsBlock extends BlockBase {

  #[\Override]
  public function build(): array {
    return [
      '#theme' => 'item_list',
      '#items' => [],
      '#cache' => ['tags' => $this->getCacheTags()],
    ];
  }

  #[\Override]
  protected function blockAccess(AccountInterface $account): AccessResultInterface {
    return AccessResult::allowedIfHasPermission($account, 'access content');
  }

  #[\Override]
  public function getCacheTags(): array {
    return array_merge(parent::getCacheTags(), ['node_list:article']);
  }

}

Every method here comes from BlockPluginInterface or from the traits BlockBase uses, so each override is real. Now misspell one name, say getCacheTag(), and the class fails to load with the compile error above instead of shipping a block that never invalidates.

Where it pays off: upstream renames

The typo case is the small win. The bigger one is refactors you do not control. If a parent class renames or removes a method in a minor release, a subclass without the attribute keeps loading fine, and your override just stops running. With the attribute, the same update gives you a hard error the first time the class is loaded, which you will see in CI rather than in production. The same applies to your own interfaces: rename a method on the interface and every implementing class that carries the attribute tells you immediately.

composer update drupal/core-recommended
vendor/bin/phpunit --group my_module

That second line only helps if your tests actually load the classes, so make sure each plugin gets at least a kernel test that instantiates it.

Gotchas

I add it to every override in new code, but I would not blanket-rewrite an old codebase in one commit. Constructors are exempt, so __construct() stays unmarked. I have not checked every edge case for trait-provided methods, so verify that on your target PHP version before relying on it there. And it does nothing for a method you intend to be new: if you add it by habit to a helper that does not override anything, you get exactly the error you wanted to see, just for the wrong reason. Pair it with a static analyser and you have two independent nets under the same mistake.

BM
Blue Moose
The moose behind Blue Moose. Full-stack PHP developer — Drupal by day, Symfony by night, tests always.