The class loader in Drupal 8 implemented support for the PSR-0 standard previously.
The class loader has been changed to use Package-Oriented Autoloading, the PSR-4 standard, by default. All core modules have been migrated to PSR-4. See the Documentation page for PSR-4 in Drupal 8
This change does not affect your namespaces and use statements in code; it affects file locations only.
Before (PSR-0)
/modules/my_module/my_module.info.yml
/modules/my_module/lib/Drupal/my_module/MyModuleClass.php
/modules/my_module/lib/Drupal/my_module/Entity/MyEntity.php # Plugin
/modules/my_module/lib/Drupal/my_module/Tests/MyEntityTest.php # Simpletest
/modules/my_module/tests/Drupal/my_module/Tests/Entity/MyEntityTest.php # PHPUnit
After (PSR-4)
/modules/my_module/my_module.info.yml
/modules/my_module/src/MyModuleClass.php
/modules/my_module/src/Entity/MyEntity.php # Plugin
/modules/my_module/src/Tests/MyEntityTest.php # Simpletest
/modules/my_module/tests/src/Entity/MyEntityTest.php # PHPUnit
The effective changes:
-
Autoloaded classes are moved directly into the
./srcsubdirectory:- ./lib/Drupal/my_module/MyModuleClass.php + ./src/MyModuleClass.phpThis applies to all classes in your namespace. Classes in sub-namespaces are retained in subdirectories as-is. In short:
$ mkdir src $ git mv lib/Drupal/my_module/* src/ -
PHPUnit tests are moved directly into the
./tests/srcsubdirectory:- ./tests/Drupal/Tests/my_module/MyModuleTest.php + ./tests/src/MyModuleTest.phpThis applies to all classes in the
Testsnamespace. Classes in sub-namespaces are retained in subdirectories as-is. In short:$ mkdir -p tests/src $ git mv tests/Drupal/my_module/Tests/* tests/src/
\Drupal\Core and \Drupal\Component
Files located in core/lib - i.e. classes in the \Drupal\Core and \Drupal\Component namespace - are not moved. They remain in the same place after this change.
Temporary transition period
For a certain period, both PSR-4 and PSR-0 autoloading will be supported in parallel, so as to facilitate conversions in Drupal core and contributed modules. The length of this period is being discussed at #2247287: Drop automatic PSR-0 support for modules. To ensure forward compatibility, it is recommend that modules are converted to the PSR-4 file layout at the earliest opportunity.
Note that PSR-0 theoretically supported underscores (_) as namespace separators, such that Foo_Bar_Baz.php could contain a Baz class in the Foo\Bar namespace. With the switch to PSR-4 this is no longer supported. Because module classes in lib directories - i.e. those that are still in the PSR-0 location - are internally registered using the PSR-4 mechanism as well this applies to those classes as well. Because such file names violate Drupal coding standards this should generally not affect anyone.