Problem/Motivation
State what you believe is wrong or missing from the current standards.
There are places in Drupal where we have string values referring to class names.
Often these values are initialized with regular string literals. This makes it hard for an IDE (I only tried with PhpStorm, no idea about others) to
- list these string literals in "find usages" for a class.
- provide auto-completion when writing this code.
- warn you about non-existing or misspelled classes.
- include these string literals in refactor/rename.
PHP provides a syntax where all this is supported: "::class".
I propose that this becomes the recommended way of referring to class names as strings.
Example
In Drupal\Core\Cache\ApcuBackendFactory::__construct():
Current code:
$this->backendClass = 'Drupal\Core\Cache\ApcuBackend';
could be replaced with
$this->backendClass = \Drupal\Core\Cache\ApcuBackend::class;
or better
[..]
use Drupal\Core\Cache\ApcuBackend;
[..]
$this->backendClass = ApcuBackend::class;
Side note
The same problem exists with procedural callbacks, or string method names. Unfortunately there is no foo::function syntax in PHP. I personally always put a @see comment on top of string literals referring to functions or callbacks, to facilitate "find usages". But this should be dealt with in a separate issue.
Benefits
If we adopted this change, the Drupal Project would benefit by …
- … making it easier to find all usages of a class (in IDEs or similar).
- … providing auto-completion when writing this code.
- … warn you about non-existing or misspelled classes.
- … include these string literals in refactor/rename.
Three supporters required
- https://www.drupal.org/u/drunken-monkey (2025-02-23)
- https://www.drupal.org/u/borisson_ (2017-10-14)
- https://www.drupal.org/u/quietone (2026-04-19)
Proposed changes
1. PHP coding standards
Current text
None (new section)
Proposed text
Referencing classes
Whenever a class is referenced directly in code, the
Classname::classsyntax should be used, as in these examples:class_alias(TranslatableMarkup::class, TranslationWrapper::class, TRUE); uasort($info, [SortArray::class, 'sortByWeightElement']); protected readonly string $pluginDefinitionAttributeName = Plugin::class;
2. PHP coding standards » Constructor calls
Current text
Note that if the class name is a variable, the variable will be evaluated first to get the class name, and then the constructor will be called. Use the same syntax:
$bar = 'MyClassName'; $foo = new $bar(); $foo = new $bar($arg1, $arg2);
Proposed text
Note that if the class name is a variable, the variable will be evaluated first to get the class name, and then the constructor will be called. Use the same syntax:
$bar = MyClassName::class; $foo = new $bar(); $foo = new $bar($arg1, $arg2);
Remaining tasks
Create this issue in the Coding Standards queue, using the defined templateAdd supporters- Create a Change Record
- Review 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
Issue fork coding_standards-2801959
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
donquixote commentedComment #3
drunken monkey+1
Didn't know about this feature until a few months ago, otherwise I would have used it in a lot more places in my modules. It's really much more practical with an IDE. (Another example: clicking the class name to go to the class.)
(Of course, we need a note to not use this in D7, since the feature was only added in PHP 5.5.)
Comment #4
donquixote commentedComment #5
pfrenssen+1
This is one of the rare cases where we can actually have a tangible benefit (improved code navigability) through specifying the coding standards.
Comment #6
dawehnerTotally +1
Comment #7
klausi+1
The next step is to provide a concrete proposal in the issue summary what exactly we put into the coding standards document and where.
The ::class syntax is a PHP 5.5 feature, so we also need to mention that this only applies to Drupal 8. Drupal 7 code should not use this syntax.
Comment #8
bojanz commentedOh, yes. +1
Comment #9
borisson_+1.
I added a short example of how I think this should go in the rules. This is the first time I tried providing such a text, so it's probably not very clear and/or not long enough.
In any case, I'd love to see us doing this.
Comment #10
effulgentsia commentedIs this something that a Coder rule could be written for? Seems like a tricky one.
Comment #11
megachriz@effulgentsia
Something like the follow regex?
This will match the following strings:
The regex should probably be adjusted a bit to ensure it doesn't match code in annotations and perhaps even 'normal' comments.
Comment #12
donquixote commentedOr D7 contrib modules that explicitly require PHP 5.5+
Comment #13
quietone commentedComment #14
drunken monkeyUpdated the IS and added myself as a supporter. I didn’t find any appropriate section in the PHP coding standards so I propose adding a new one.
Btw, found this nugget in the class naming conventions which isn’t really up to date anymore (emphasis by me):
Comment #15
drunken monkeyComment #16
borisson_Added my support as well, picked the date I originally posted a +1 on this issue.
Comment #17
borisson_I'm not sure how we manage this, we've had multiple +1 statements on this issue, but we've moved to the supporter model in the IS, do we automatically count those as well?
Comment #19
quietone commentedComment #20
quietone commentedNeeds work for a change record. Also needs review of the MR.
Comment #21
quietone commentedComment #22
andypost+1 as it helps navigation and better speed for php
Comment #23
borisson_@webflo mentioned that https://getrector.com/rule-detail/string-class-name-to-class-constant-re... exists, that will help with the conversion.
Reviewed the merge request, that looks great. Next thing is writing the change record.
Comment #24
borisson_Added a draft change record.