API/HookManager

From aMember Pro Documentation
Revision as of 08:52, 28 June 2011 by Alex (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

aMember Pro provides flexible mechanism to change or add new behaviour to existing actions. It utilises well-known [Observer] programming pattern.

To wait for an event, code must setup a "hook",. Then, on some occasion, core code or add-on code(!) sends Events (instances of Am_Event class or successors) to all interested parties. Parties handle events by executing pieces of code and for some events, by adding return values to event object. Then, event object is returned to called core code where it can be inspected and used. It is long to explain, but easy to understand by example:

<?php
// define handlers for an event
function my_custom_function1(Am_Event $event) { $event->addReturn($event->getX() + 1); }
function my_custom_function2(Am_Event $event) { $event->addReturn($event->getX() + 2); }
 
// setup hooks
Am_Hook::manager()
   ->add('my_custom_event', 'my_custom_function1')
   ->add('my_custom_event', 'my_custom_function2');
 
// now raise the event with variable x = 5 and get returned event object
$event = Am_Hook::manager()->call('my_custom_event', array('x' => 5));
 
// now check the return. it will be array(5+1, 5+2) == array(6, 7)
print_rr($event->getReturn());