Problem/Motivation

This has been a discussion point in the MR's of our team for our projects. Mainly about functions calls like \Drupal:: in none name-spaced files (e.g. .module, .install, .api,...).

I would usually write my code with the leading backslash.

$facet_manager = \Drupal::service('facets.manager');

But then a team member would come in saying the leading backslash is not required as we're not in a namespace. Looking at Drupal core, I can see a mix of both being used, so I think it's good if we could standardise this as a standard.

Core example where it is done with a \ : https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/autom...

Core example where it is done withouth a \ : https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/toolb...

Happy to say thought that there are not that many cases without the \ in Drupal Core. I did a code search today on the 11.x branch in PHPStorm (since it's not possible to search on Drupal:: with GitLab code search) and only found 2 code occurrences. Screenshot attached.

Benefits

By enforcing this change, function calls like \Drupal:: are written the same everywhere when not in a namespace.

Three supporters required

  1. https://www.drupal.org/u/borisson_ (2025-01-23)
  2. https://www.drupal.org/u/drunken-monkey (2025-01-23)
  3. https://www.drupal.org/u/sophie.sk (2025-02-21)
  4. https://www.drupal.org/u/Kingdutch (2026-04-28)

Proposed changes

Provide all proposed changes to the Drupal Coding standards. Give a link to each section that will be changed, and show the current text and proposed text as in the following layout:

1. Namespaces » Using classes

Current text

Classes and interfaces without a backslash \ inside their fully-qualified name (for example, the built-in PHP Exception class) must be fully qualified when used in a namespaced file. For example: new \Exception();. Do not use global classes.

Proposed text

Classes and interfaces without a backslash \ inside their fully-qualified name (for example, the built-in PHP Exception class) must be fully qualified. For example: new \Exception();. Do not use global classes.

2. Repeat the above for each page or sub-page that needs to be changed.

Remaining tasks

  1. Create this issue in the Coding Standards queue, using the defined template
  2. Add supporters
  3. Create a Change Record
  4. Review by the Coding Standards Committee
  5. Coding Standards Committee takes action as required
  6. Discussed by the Core Committer Committee, if it impacts Drupal Core
  7. Final review by Coding Standards Committee
  8. Documentation updates
    1. Edit all pages
    2. Publish change record
    3. Remove 'Needs documentation edits' tag
  9. If applicable, create follow-up issues for PHPCS rules/sniffs changes

For a full explanation of these steps see the Coding Standards project page

CommentFileSizeAuthor
#4 Screenshot 2025-01-23 at 09.43.52.png224.91 KBbramdriesen
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

bramdriesen created an issue. See original summary.

bramdriesen’s picture

Issue summary: View changes
borisson_’s picture

Issue summary: View changes

Added a very basic line of what we need to add. Should be refined.

bramdriesen’s picture

Issue summary: View changes
StatusFileSize
new224.91 KB

Attaching screenshot of Drupal Core (11.x) code search. Looks like many things have been fixed already between Drupal 9/10 to 11 as I can find many more occurrences in our projects (might come from patches as well of course).

drunken monkey’s picture

Title: Coding standard to enforce backslash on function calls outside of a namespace » Coding standard to enforce backslash on global class names outside of a namespace
Issue summary: View changes

Thanks for opening this!

The current documentation on reads:

Classes and interfaces without a backslash \ inside their fully-qualified name (for example, the built-in PHP Exception class) must be fully qualified when used in a namespaced file. For example: new \Exception();. Do not use global classes.

It seems we’d just need to remove the “when used in a namespaced file” part?

Adding myself as a supporter. Also fixing the title – this is not about function calls but about references to classes (for static method calls, constructing new objects or just the ::class reference).

bramdriesen’s picture

It seems we’d just need to remove the “when used in a namespaced file” part?

Probably yes, and update coder/phpcs to check for this.

sophie.sk’s picture

This comes up in our pull requests a lot as well. My preference is to not include the backslash but I think I'm in the minority.

My real preference is for a coding standard and consistency. It looks like the precedent generally is to include the backslash, so lending my support for this change.

bramdriesen’s picture

Issue summary: View changes
Status: Active » Needs work
Issue tags: +Needs change record
borisson_’s picture

Issue tags: -Needs change record

CR Added

bramdriesen’s picture

Status: Needs work » Reviewed & tested by the community

CR looks fine to me. I guess this is RTBC then?

borisson_’s picture

Issue summary: View changes
quietone’s picture

Title: Coding standard to enforce backslash on global class names outside of a namespace » Backslash on global class names outside of a namespace
quietone’s picture

Title: Backslash on global class names outside of a namespace » Classes and interfaces without a backslash \ inside their fully-qualified name must be fully qualified

Try again for a better title

quietone’s picture

This needs an MR.

It can be enforced for exceptions with SlevomatCodingStandard.Namespaces.FullyQualifiedExceptions. Core has 3287 ERRORS across 967 files, but apparently phpcbf can fix them.

In the meeting SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions was mentioned but we don't want to change global functions in this issue.

quietone’s picture

Issue summary: View changes
Issue tags: +Needs followup

Came up in two meetings, #3576923: Coding Standards Meeting Wednesday 2026-03-04 0700 UTC and #3580167: Coding Standards Meeting Tuesday 2026-04-01 2340 UTC. There were no objections.

A followup in core should be made to implement this, per #14. It should be a child of #3553320: [meta] Fix PHP coding standards in core, stage 2.

kingdutch’s picture

Issue summary: View changes

Happy to support this. This ensures consistency regardless of where the code is read and removes the need for a developer to figure out whether they're currently in a namespaced file.

Text change and decision record look good.

quietone’s picture

Issue tags: -Needs followup

@borisson_, thanks for making the followup.

AFAIKT all the steps are complete here so I will commit this and publish the change record. I have updated credit.

  • quietone committed 4a57bff6 on main
    feat: #3501643 Classes and interfaces without a backslash \ inside their...
quietone’s picture

Status: Reviewed & tested by the community » Fixed

Thanks everyone!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

quietone’s picture

Published the change record

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.