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
- https://www.drupal.org/u/borisson_ (2025-01-23)
- https://www.drupal.org/u/drunken-monkey (2025-01-23)
- https://www.drupal.org/u/sophie.sk (2025-02-21)
- 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 notuseglobal classes.
Proposed text
Classes and interfaces without a backslash
\inside their fully-qualified name (for example, the built-in PHPExceptionclass) must be fully qualified. For example:new \Exception();. Do notuseglobal classes.
2. Repeat the above for each page or sub-page that needs to be changed.
Remaining tasks
Create this issue in the Coding Standards queue, using the defined templateAdd supportersCreate a Change RecordReview by the Coding Standards Committee- Coding Standards Committee takes action as required
- Discussed by the Core Committer Committee, if it impacts Drupal Core
- Final review by Coding Standards Committee
- Documentation updates
- Edit all pages
- Publish change record
- Remove 'Needs documentation edits' tag
- If applicable, create follow-up issues for PHPCS rules/sniffs changes
For a full explanation of these steps see the Coding Standards project page
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | Screenshot 2025-01-23 at 09.43.52.png | 224.91 KB | bramdriesen |
Issue fork coding_standards-3501643
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
Comment #2
bramdriesenComment #3
borisson_Added a very basic line of what we need to add. Should be refined.
Comment #4
bramdriesenAttaching 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).
Comment #5
drunken monkeyThanks for opening this!
The current documentation on reads:
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
::classreference).Comment #6
bramdriesenProbably yes, and update coder/phpcs to check for this.
Comment #7
sophie.skThis 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.
Comment #8
bramdriesenComment #9
borisson_CR Added
Comment #10
bramdriesenCR looks fine to me. I guess this is RTBC then?
Comment #11
borisson_Comment #12
quietone commentedComment #13
quietone commentedTry again for a better title
Comment #14
quietone commentedThis 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.
Comment #16
quietone commentedCame 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.
Comment #17
borisson_Created #3586405: Classes and interfaces without a backslash \ inside their fully-qualified name must be fully qualified
Comment #18
kingdutchHappy 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.
Comment #19
quietone commented@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.
Comment #21
quietone commentedThanks everyone!
Comment #23
quietone commentedPublished the change record