Camel-case or snake-case for Drupal code?

For some time, Drupal's PHP coding standards allows for writing variables in either snake-case (e.g. $my_variable) or lower camel-case (e.g. $myVariable).

It originally only allowed for snake-case variable names but once it accepted both, I switched to camel-case as my default.

Why? I didn't like the inconsistency of using one approach for variable names and one for method and property names in PHP classes (which were always camel-case).

I'd have had code like this with a mixture of both:

class MyClass {

  private EntityTypeManagerInterface $entityTypeManager;

  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

}

Or even more simply:

$entity_type_manager = \Drupal::entityTypeManager();

I prefer not to have to consistently think about which to use and, if possible, like to use standard approaches in different codebases whether I'm working on a Drupal project, a Symfony project, or a PHP library.

Plus, I get to use new PHP features like promoted constructor properties if everything is named in the same format.

- Oliver

P.S. Do you need immediate access to an expert Drupal Developer? With my Drupal development subscription, make unlimited requests for a fixed monthly price in less time than posting to a job board!

Was this useful?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.