Common patterns
As mentioned, all Drupal modules follow specific conventions and patterns that bring uniformity across core, contributed, and custom modules. While not exhaustive, there are some common patterns used within modules.
PHP patterns
Object-oriented PHP makes use of PSR standards (https://www.php-fig.org/psr/). PSR-4, which deprecated PSR-0, is a method of autoloading files. This is how Drupal modules, Symfony, and other PHP applications can make use of specific filenames and paths to load files. Files loaded in specific paths help register the code of specific patterns. For instance, a new service class can be placed in a src/Service
directory to register a service.
Namespaces are PHP conventions used to organize classes, which are leveraged by Drupal modules. Every auto-loaded file makes use of this pattern, as demonstrated by a namespace for a MyService
class:
namespace Drupal\my_module\Service;
Every class in a module uses the same base namespace to keep...