Is Drupal Object-Oriented?

By Deane Barker on April 28, 2010

Drupal programming from an object-oriented perspective: This is either an example of architectural brilliance, or just lazy programmers making excuses.

Adam Kalsey turned me on to this article.  In it, the creators of Drupal try to explain that while Drupal is not very object-oriented in a pure code sense, it’s object-oriented architecturally, if you transcend the code and look at the entire architecture.

Although Drupal does not make thorough use of the native OOP features of PHP, the Drupal code base and API does reflect some principles found in object-oriented programming and design.

This is a valid point?  Judge for yourself with this discussion of objects (wondering if they mean “classes” here, actually).

There are many constructs in Drupal that fit the description of an “object”. Some of the more prominent Drupal components that could be considered objects are modules, themes, nodes, and users.

[…] Modules and themes are object-like as well, filling the “controller” role in many ways. Each module is a source file, but also bundles together related functions and follows a pattern of defining Drupal hooks.

Another example, this type about polymorphism:

Nodes are polymorphic in the classical sense. If a module needs to display a node, for example, it can call nodebuild() on that node, followed by drupalrender(), to get an HTML representation. The actual rendering, though, will depend on which type of node is passed to the function; this is directly analogous to having the class of an object determine its behavior when a message is sent to it. Drupal itself handles the same introspection tasks required of an OOP language’s runtime library.

I’m not sure if I buy it yet, but it doesn’t matter.  I have seen very little in the Drupal codebase that would really benefit from more object-orientation.  Like I mentioned when I discussed Drupal last year, it’s really a different animal.  Drupal is a loose collection of code, tied together with hooks and events.  It’s unlike much that I’ve ever worked with before.

What This Links To


  1. As much as I endorse Drupal for sites that incorporate an amalgam of standard or quasi-standard features, the code base is a big turn-off compared to, say, CakePHP (which I vastly prefer using whenever possible).

    In Drupal’s defense, I think it’s just guilty of the same old software scenario–it starts with a simple purpose and gets much bigger, much faster than the creators realized. It would be impossible now to shoehorn Drupal into PHP’s built-in OOP features. Luckily, CakePHP doesn’t have those constraints and is unafraid of making fairly big changes from version to version, if it better supports OOP and the MVC architecture. It really is a pleasure to use.

  2. It’s unlike much I’ve ever worked with before, but it does share some attributes with things I hope to never encounter again.

Comments are closed. If you have something you really want to say, tweet @gadgetopia.