I'm looking to start building my modules as classes now that lazy/auto-load are in Drupal 7, and I'd love to look at some modules (contrib or core) that are doing a great job of it.

Is there a creative way for integrating a class as hooks? Do I implement each feature as a ctools plugin?

Any passers-by should note that D7 has new files[] declarations in module .info files for auto/lazy-loading classes/interfaces: Writing .info files (Drupal 7.x).

The following is a good read to get the motivations and the constitution (as it were) from the Drupal community Drupal programming from an object-oriented perspective.

3 Answers 3


A module is not a class by Crell is what you should read first.

The biggest OOP part in Drupal 7 is the new database abstraction layer (designed by the very same Crell and others). That implements many patterns which can and should be re-used.

For example:

Something that is still controversaly discussed is Information hiding. DBTNG implements that by making class properties protected and disallowing direct access. On the other hand, we have hook_query_alter() and they can be changed almost freely with various by reference getter methods.

  • great answer, tons of great resources to update my thinking when structuring my module code!
    – electblake
    Jul 5, 2015 at 16:14

This is an interesting question.

The idea of a Drupal module as a class is very interesting. However even in D7 modules simply implement hook functions which are checked for by name, so even if you create a class for your module you will still need to implement hook functions to invoke them.

However many modules use classes internally, and expose functionality via these classes. The most obvious example is views. So that may be a good place to look. However be aware that many modules use classes in their own way, so what you learn from looking at views may not always be applicable.

  • Thanks for the insight, and I did presume that I'd still need to have hook_functions to register properly within Drupal but I am curious at how modules implement it. I'll take a look at the views module (and hopefully not get lost) - please update your answer if you find any more isolated cases/examples of patterns you find :D
    – electblake
    Apr 8, 2011 at 14:32

Entity API (the contrib module) allows you to declare an "entity class" for each of your entity types, so all code related to your entity (create, load, save, access, etc) can go in a class (see how Organic Groups does it, for example).

That said, don't overdo it. ctools plugins are a good thing when you need them (you're writing the next Views, Panels or Rules), but using classes everywhere just for ideological purity won't do you much good.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.