What way should I use to load a file from a module?

I am writing a module which is divided into 3 parts

  • Custom core will be in the module which contains basic functions required for project
  • Custom notification section will handle the notifications to show on site (It has a section on site where user can see all notifications.)

  • Custom mail will handle mail (Users will get email on each activity.)

So I have now a module file and 2 include files, but I am confused on how to include them in .module file.

There are 3 different methods available, but I am not sure which one is best and what the major differences between them are.

  1. require_once drupal_get_path('module', 'custom_module')
  2. files[] = mail.inc
  3. module_load_include('inc', 'custom_module', 'mail.inc')

I tried them, and they all are working fine for now; I don't have much experience to guess future issues.

  • You can include by using function module_load_include – Nishant May 19 '15 at 10:56
  • yes i can use it but why it is best ? – Vikram May 20 '15 at 3:39

module_load_include() is a wrapper around require_once, with some sugar, so those are essentially equivalent. In the Drupal world, it would make sense to use the Drupal-specific function.

The files[] directive is for class autoloading - you don't use it to include files as such, just to make classes in those files available automatically when you need them.

You shouldn't use module_load_include() at the top of a module (or any other) file, it should be used only after Drupal has been bootstrapped. From the docs:

Do not use this function in a global context since it requires Drupal to be fully bootstrapped, use require_once DRUPAL_ROOT . '/path/file' instead.

  • so i can use require_once at top of the .module file ? – Vikram May 19 '15 at 12:16
  • Absolutely, that's the recommendation from the core developers – Clive May 19 '15 at 12:24
  • Can we safely use drupal_get_path() (common.inc) in combination with require_once, for getting the path to the module folder? Or is that also only loaded in a full bootstrap? – Neograph734 May 20 '15 at 11:21
  • @Clive could you help me out here? – Neograph734 May 22 '15 at 8:17

The best way for drupal to know you have included files is having it in the files[] directive, as Clive says this is perfect for classes, but you can also use it for any files.

Using module_load_include() you can add some loading logic, you can load also files from another module, and for best performance you can load them from within a function or a conditional structure. That way is only loaded when you are using it.

  • 1
    No, you cannot use files[] for any files, since Drupal will use it just to load classes. – kiamlaluno May 21 '15 at 11:50
  • I'm using it now in a drupal 7 module – David Lemon May 21 '15 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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