Skip to main content

Drupal made a significant shift towards adopting the object-oriented programming (OOP) paradigm starting with its major release, Drupal 8 many many moons ago. I'm sure many wanted to know why. Drupal addressed the criticism of the lack of explicit use of object-oriented programming (OOP) in Drupal's source code and API by explaining the reasons behind their design choices. Here are the key points they mentioned:

  1. Immaturity of PHP's OOP features: At the time Drupal was designed, PHP's support for object-oriented constructs was less mature. Drupal was built on PHP 4, and most of the improvements in PHP 5 were related to its object-oriented features. As a result, Drupal did not extensively utilize PHP's native OOP features.

  2. Modular and performance considerations: Drupal's code is highly compartmentalized into modules, with each module defining its own set of functions. The inclusion of files is handled inside functions, and Drupal aims to load as little code as possible per request for performance reasons. This design choice resulted in functions being defined inside other functions, which is allowed for bare functions but not for class declarations in PHP. Therefore, the inclusion of files defining classes had to be done at the "top-level," either slowing down the code by always including class files or adding a large amount of logic to the main index.php file.

  3. Adoption of OOP design aspects: Despite not utilizing PHP's class construct extensively, Drupal attempted to adopt certain OOP design aspects that were not yet supported or fully supported by PHP's OOP system at the time. One example is the Drupal theme system, which relies on an OOP concept similar to "categories" in Objective-C or "extension methods" in C#. Drupal 7 introduced some usage of PHP's class system in a few modules, but the central structure of Drupal remained the same, and most of Drupal still did not use the PHP class construct.

Although Drupal's code base does not align with a traditional OOP approach, it does exhibit object-oriented principles and design patterns in its architecture. Some of these include objects (such as modules, themes, nodes, and users), abstraction through hooks, encapsulation through naming conventions, polymorphism with nodes and themes, inheritance in modules and themes, and the use of design patterns like Singleton, Decorator, Observer, Bridge, Chain of Responsibility, and Command.

The developers acknowledge that there is room for improvement, such as applying encapsulation more rigorously and strengthening inheritance in the system. However, they also highlight that Drupal is not limited to a single programming paradigm. While it is primarily built in a procedural language (PHP without classes), Drupal incorporates concepts from various paradigms, including relational programming, event-driven programming, aspect-oriented programming, and object-oriented programming.

x

Work

Therefore logo
80 Atlantic Ave, Toronto, ON Canada
Email: hello@therefore.ca
Call us: +1 4166405376
Linkedin

Let us know how we can help!