If the implementation of hook_install() of my module needs to use a class defined in the module file, should the code first load the module file with drupal_load()?

I would think that when the hook is invoked, the Drupal registry has not been yet updated to contain any reference to the PHP classes defined in the modules used by my module, and which are referred in the .info file. I looked at Drupal code, but I could not find any proof for what I think Drupal does.

Should I use drupal_load() or the file will be loaded automatically when I try to create an instance of the PHP class defined in the module file? Is it different with hook_update_N()?


No need, it's already loaded. Check http://api.drupal.org/api/drupal/includes--module.inc/function/module_enable/7

if ($existing->status == 0) {
  // Load the module's code.
  drupal_load('module', $module);
  // …

Or you can just:

require_once dirname(__FILE__) . '/mymodule.module';

Anywhere inside of your .install file (top is better).

This is not the "Drupal way" but it will work seamlessly whatever happens.

The proper method would be:

drupal_load('module', 'mymodule');

Inside all hook_install() and hook_update_N() implementations that needs the class. But I'm not sure it will work at install time.

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.