By Jesse McCarthy on
I'm unclear about how I should be calling a function in one module from another.
E.g. if ABC and XYZ are modules, I want to call XYZ_whatever() from ABC.
So should I call XYZ_whatever() directly, or use module_invoke( 'XYZ', 'whatever' ) ?
The description of module_invoke() says "Invoke a hook in a particular module." Is "hooK" being used as a synonym for "function" or does it mean hook as in one of these functions? http://api.drupal.org/api/4.7/group/hooks
Comments
I think one typically only
I think one typically only uses module_invoke() when you don't know ahead of time what the function is that you want to use. In that sense, you onlt use it with a "hook".
In contrast, if you know the name of the function when you're writing you codee, just call it. For example, some of the modules I maintain are dependent on other modules (e.g. Organic Groups or Event) and call directly the functions from these modules.
see, for example:
http://drupal.org/project/og_mandatory_group
Which has the code like the following (note the call to og_save_subscription(), a function defined in og.module not in my module):
edit:
see also: http://groups.drupal.org/node/2312#comment-6364
Where there's the suggestion to use module_invoke() more generally, since it will prevent a fatal error if the module you're trying to call does not exist.
---
Work: BioRAFT
Thanks for the feedback and
Thanks for the feedback and for looking into it further.
I don't know what the protocol is for addressing it, but I'd highly recommend updating that API documentation to reflect that since it is very confusing to say that it is for "hooks" when there is actually something in Drupal called hooks and that apparently is not all this function relates to. It would also be helpful to find that information in the documentation for module developers.
Thanks!