Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

4 Answers 4

up vote 13 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
    
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 '11 at 14:32
add comment

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.

share|improve this answer
add comment

as Berdir says, do not build your modules as classes. If you need to build a Class, or reuse a class, is because you are building a library.

So:

  1. install libraries module,
  2. use autoload hook

More info: http://engineeredweb.com/blog/10/5/3-tips-using-external-libraries-drupal/

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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