I was wondering when you need to use module_load_include()
or require_once
to include files which are located within your module.
-
Drupal is opensource, you can do a check on how is this function written – ajreal Sep 01 '11 at 13:10
-
@ajreal: it's still a legitimate question. Even the [Drupal manual page](http://api.drupal.org/api/drupal/includes--module.inc/function/module_load_include/6) doesn't give a definitive case for using it over `require_once`. In fact, a similar question was asked in the comments on that page, and the answer was "it's more Drupal-ish to use it", which isn't really a very helpful answer when you're trying to work out what is best practice in any given situation. – Spudley Sep 01 '11 at 13:24
-
@ajreal: I now what is does, i'm just wondering if you need to use module_load_include if you want to include files from within the same module. – Frederic Sep 01 '11 at 13:25
2 Answers
The key thing that the Drupal module_load_include()
function does over and above the standard PHP require_once
is that it references the module's path when locating the file, using drupal_get_path()
.
If you were to use require_once
, you would have to do this bit yourself.
The other thing it does is check that the file exists prior to trying to include it, which is handy for avoiding fatal crashes, but rather pointless if you're going to get one anyway when you try to call the functions you tried to include. This is handy though for allowing you to produce more meaningful errors.
At the end of the day, module_load_include()
is really just a little utility function provided by Drupal to make things slightly easier for themselves. If you know where the file is located, and you know it exists there, there's very little need to use the Drupal function; you may just as well use require_once
.
-
2To make the explanation more explicit, your module could be installed in sites/all/modules/, sites/all/modules/contrib/, sites/site.com/modules etc. module_load_include() will know where your module is installed and use the correct path. – Berdir Sep 01 '11 at 14:58
-
@Berdir: absolutely correct. On the other hand, if you're including it from within another file inside the same module, you only need to know the relative path, so `require_once` would suffice. `module_load_include()` would typically be better used when including a module file from outside of that module (ie as needs to be done by the code in the Drupal core) You're quite right though; both have their place. – Spudley Sep 01 '11 at 15:02
-
5@Spudley the relative path is always starting from the initially requested file, which is index.php. To actually include a file from your own module, you'd have to use a trick like dirname(\__FILE__) to get the location of your .module file. – Berdir Sep 02 '11 at 08:46
module_load_include requires Drupal to be loaded fully (Fully Bootstrapped).
syntax: module_load_include($type, $module, $name = NULL);
Eg: module_load_include('inc','module_name','file_name');
if u want to use this function in a global context then use require_once
require_once doesn't need it.
Eg: require_once DRUPAL_ROOT . '/path/file' .

- 292
- 2
- 8