PHP 5.3 introduces namespaces to the language. Use of this mechanism should be preferred to
include
or include_once
or require
or require_once
because it solves two common problems:
- it avoids name collisions
- it provides the ability to create alias which improve readability of the code
Starting from its version 8, Drupal is relying on namespaces to be compliant with PSR-4 standard. Drupal’s modules should be compliant with
PSR-4 standard and therefore should no longer rely on include
or include_once
or require
or
require_once
functions.
Noncompliant Code Example
require_once('./modules/vegetable/src/Entity/Tomato.php');
Compliant Solution
use Drupal\vegetable\Entity\Tomato
Exceptions
This rule doesn’t raise issues on autoload.php
or ScriptHandler.php
files.