CoreEvent

Filepath: phork/php/core/CoreEvent.class.php and phork/php/app/AppEvent.class.php

The CoreEvent class is used to register and run events throughout the application. An event consists of one or more callback functions. Its most common use is to register and run the hooks in the bootstrap.

To register an event the register() method should be passed an event name followed by the same parameters as call_user_func_array(), which is a callback function, and (optionally) the array of arguments to pass to the callback function.

To run an event the run() method should be passed an event name. By default it's not considered an error if the event being run doesn't exist, however an optional second argument can force an exception to be thrown for a non-existant event. The run() method returns an array of all the results from each event merged into a single array. This means if multiple events return the same results then the final result will only contain the last event's results.

The event class is a singleton meaning there can only be one instance of it at a time.


Basic Example

//register an event to run AppDisplay::getInstance()->appendString('content', 'bar');
AppEvent::register('foo', array(AppDisplay::getInstance(), 'appendString'), array('content', 'bar'));

//run the foo event which will append the string "bar" to the content output
AppEvent::run('foo');

Results Example

function myFoo() {
    return array(
        'Foo' => 'foo',
        'Baz' => 'baz'
    );
}

function myBar() {
    return array(
        'Bar' => 'bar',
        'Baz' => 'abc'
    );
}

//register the foo event to run the myFoo() and myBar() functions
AppEvent::register('foo', 'myFoo');
AppEvent::register('foo', 'myBar');

//run the foo event and get the results
$arrResult = AppEvent::run('foo');

print $arrResult['Foo']; //foo
print $arrResult['Bar']; //bar
print $arrResult['Baz']; //abc