Bitrix Site Manager

Interaction of modules

Modules can interact in two ways: explicitly (via the direct call) and implicitly (via the event system).

Explicit interaction

Explicit interaction stipulates:

Example of explicit interaction:

<?
// include module mymodule
if (CModule::IncludeModule("mymodule"))
{
    // call its method
    CMyModuleClass::DoIt();
}
?>

Interaction via events

Each module can expose the interface for the implicit interaction with other modules through the set of events. The interaction allows for maximum independence of modules. A module knows nothing about features of another module, but they can interact via the event interface.

The scheme of handling the events is as follows: the module initiating an event should perform the following operations where an event is to occur:

The module that needs to execute any action upon the event arrival, in its turn should do the following:

Thus, event is an action which implies collecting all handlers of the event and executing them one by one.

A striking example of such interaction is the interaction of the system modules with the Search module. The Search module has no information about features of other modules, neither of storing nor of processing peculiarities; it just exposes interface for the data indexing. Any system module, which is to be indexed by the Search module, registers the OnReindex event handler. Each handler returns the indexing data, which is used by the Search module to populate its database.

Note!
A list of the Kernel module events is available in the section Events.

Example of interaction via events:

<?
// register handler:
// when the event OnSomeEvent fires in the module init_module,
// the method CMyModuleClass::Handler 
// of the module handler_module is called

RegisterModuleDependences(
    "init_module", "OnSomeEvent",
    "handler_module", "CMyModuleClass", "Handler"
    );
?>
<?
// arbitrary function of the module init_module,
// that generates the event

function MyFunction()
{
    // code generating the event
    // the registered handlers are collected
    $rsHandlers = GetModuleEvents("anothermodule", "OnSomeEvent");
    while($arHandler = $rsHandlers->Fetch())
    {
        // execute one by one
        if(!ExecuteModuleEvent($arHandler, $param1, $param2))
        {
            // if the handler returns false,
            // print "I can't do it..."
            return "I can't do it...";
        }
    }
    return "I have done it!";
}
?>
<?
// handler

class CMyModuleClass
{
    function Handler($param1, $param2)
    {
        if($param1=="delete all")
            return false;
        return true;
    }
}
?>