Objected-oriented programming conventions

DrupalTM Blog

Welcome to our Drupal blog! Stay up to date with the latest Drupal news, best practices, and techniques.

Q1: What conventions and standards apply to PHP code in Drupal?

  • A: The conventions and standards for PHP code in Drupal include the usage of object-oriented programming (OO) principles, employing PHP classes, interfaces, and traits. The code should adhere to PSR-4, namespaces, and follow specific class naming conventions.

Q2: What are the requirements for organizing classes in Drupal?

  • A: Each class in Drupal must reside in its own file and follow proper namespacing rules. Module-defined classes should have a namespace starting with \Drupal\module_name, and for Drupal Core classes, the namespace should be \Drupal\Core or \Drupal\Component.

Q3: How does Drupal structure the directories for classes based on their namespaces?

  • A: The directory structure for classes is determined by the namespace. Module-defined classes follow a structure like src/foo/bar within the main module directory, while Drupal-wide classes have a structure like core/lib/Drupal/Core/foo/bar.

Q4: Are there any specific annotations associated with Drupal classes?

  • A: Yes, some Drupal classes have annotations added to their documentation headers. These annotations are further discussed in the Annotation topic for more detailed information.

Q5: Are there any specific requirements for plugin discovery in Drupal?

  • A: Yes, standard plugin discovery in Drupal necessitates particular namespaces and annotations for most plugin classes. More details on this can be found in the Plugin API topic.

Q6: Where can I find project-wide coding standards for Drupal’s object-oriented code?

  • A: Project-wide coding standards for Drupal’s object-oriented code, including naming conventions, can be found in the provided link: https://www.drupal.org/node/608152.

Q7: Is there documentation available for the standards and conventions related to classes in Drupal?