Using Other Modules

This page covers the three ways one module can use another: pack_module(), init_module(), and calling shared static methods from another module's Common class.

Before using any of these, declare the module you want to use as a dependency of your own module. Uninstall your module from the epesi Administration panel, then edit <MyModule>Install.php and add the module to the requires($v) method:

public static function requires($v) {
    return array(
        array('name' => '<required_module_name>', 'version' => '<version_number>'),
        array('name' => '<another_required_module_name>', 'version' => '<version_number>'),
        ...
    );
}

Reinstall your module once that's done. Which of the three methods below you actually use depends on how the module you're including works.

pack_module()

The simplest method is to pack the module:

$child_module = & $this->pack_module('module_path/module_name', $args, $func, $c_args, $name);

This creates a module instance and calls a function $func (body by default) with the parameters given in $args (null by default). $c_args are passed as arguments to the module's constructor, and $name is a unique id you can assign to the instance to keep it distinguishable from others. The call returns the module object, which you can use to call its methods — but you often won't need to, so you don't have to assign the result to a variable.

Base/Lang is an example of a module used this way.

init_module()

Use init_module() when you need to hold on to the module object and configure it before displaying it:

$child_module = & $this->init_module('module_path/module_name', $args, $name);

This creates a module instance and calls a function $func (body by default) with the parameters given in $args (null by default). $name is a unique id you can assign to the instance. Unlike pack_module(), you need to keep the returned module object, since you'll use it to configure the module and then display it:

$this->display_module($module_object, $args, $func);

Or, if you want the module's content returned instead of displayed directly:

$this->get_html_of_module($module_object, $args, $func);

$args is the list of arguments for the function $func (body by default) that gets called.

Libs/QuickForm is an example of a module used this way.

module_common

Many modules expose simple static functions that perform a quick action without printing any output. These belong in the module's Common class. All methods placed in a Common class should be static, and you shouldn't pack or initialize a module from within its own Common class.

Base/ActionBar is an example of a module used this way.