By jurgenhaas on
In one of my modules I'm using hook_init() to include a couple of .inc files that contain several functions that are used widely in my module. This is why I'm loading them in hook_init() and not individually at all the possible places where they were required.
Now a customers reported a problem that an undefined function was called, one of which is defined in one of those inc files.
I don't understand that. OK, hook_init is not called on cached pages according to the api documentation but what's a better way of including several resources all the time? Do I have to use hook_boot instead?
Comments
Why not just place those
Why not just place those functions in the .module file?
Because the module would grow
Because the module would grow into a big huge file which is difficult to manage. I like to keep files small and managable and separate the functions by logic into topics that relate to each other. It's not a must but I think it's better program design.
And alternative approach is
And alternative approach is to place "stubs" in the .module file for those functions you generally want available but do not want the whole function in the .module file. Something like this
or
This has the advantage over using hook_init() that it only loads files as needed. You can read about module_load_include at http://api.drupal.org/api/function/module_load_include/6
Thanks, that sounds good. The
Thanks, that sounds good. The only "downside" is that depending on the size of the .module you have to have the same line module_load_include() pretty often. I tried using this function in hook_boot() but ended up is a WSOD.
I would only use for API
I would only use for API functions, that is functions that expose functionality to the "outside" world. Unless you are implementing a kitchen sink module the number of API functions should be limited. Note that menu entries (hook_menu) can already specify a file to include.