I have a module that provides about two dozen pages (in a site with 1000s of pages). I have a file that contains a number of large functions. I want to include this file on every page used by my module. My options appear to be:

  1. Call module_load_include in every page callback - seems a slight pain
  2. Add the file option to every item in hook_menu (which would be fine as long as I don't want the page callbacks to go in a separate files from my .module file)
  3. Wrap all the functions in a class, include the file using files[] in my .info file and then call the functions like myclass::myfunction() [which sorta seems a slight misuse of the class system as most functions are returning arrays of data unconnected to each other]
  4. Load the file in hook_init, which means it would be loaded for every page in my site, not just the dozen specialist pages

It looks like 3 may be the best bet, but I'm just wondering if I'm missing something that allows me to include the file in one place just for my module pages without lots of duplicate calls to module_load_include and without using classes.

  • Wherever you'll load the page, you can always check the URL to make it conditional. So from the top fo my head I'd go for 4 and test URL chunks and include_once when you're on the correct path.
    – Countzero
    Commented Jun 1, 2012 at 9:35

2 Answers 2


If the file needs to be included on every page, the correct way is to use PHP's include or require, I don't think the fact that this is in a Drupal site should really make any difference.

I've seen plenty of modules that literally put the following at the top of the module file:


If you really want to do it a 'Drupal' way, you can use module_load_include() (which just wraps around require_once internally), directly at the top of the file instead of inside your page callback. This is what I tend to do normally.

module_load_include('inc', 'MYMODULE', 'MYMODULE.something');

Adding the file in hook_init() is perfectly fine too, I can't think of any justification to use one over the other, it would be down to personal preference.

  • 1
    I assume that sticking an include at the top of the module file would be run on every page, similar to hook_init(). I suspect the best bet is as Countzero says, and use hook_init and test the path. (I am also pretty sure I'm overthinking this, as I suspect any opcode cache worth it's salt means that including my 20k function file on irrelevant pages isn't much of a hit)
    – Apemantus
    Commented Jun 1, 2012 at 10:17
  • Yeah I think you might be over-optimising a bit there :) For the record, you can easily test $_GET['q'] for the URL directly in the file too, so there's no need to implement hook_init(). I can't see any benefit in doing that, it adds an extra hit (however small) to the page load, and if you're worried about performance every little bit helps
    – Clive
    Commented Jun 1, 2012 at 10:20

You could put the code into classes, either as static methods or full-on OOP.

Then you can use autoload to include those files when they are needed. Either using the files[] key in the *.info file, or using xautoload + PSR-0.

The benefit is that those class files will really only load when needed, and you don't need any manual include.

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.