$result //php shape: function call( &$args, &$result ) //APS - accumulator passing style foreach( array('before', 'at' ,'after') as $phase ) { while(current( $hooks[$hook][$phase] )) { $cb = key( $hooks[$hook][$phase] ); $cb($args, $result ); //swap the two next( $hooks[$hook][$phase] ); } } return $result; case 'add': $hooks[$hook][$phase][$args] = true; break; case 'remove': unset( $hooks[$hook][$phase][$args]); break; } } //adding sugar function hook_before( $hook, $cb ) { invoke( $hook, $cb, 'add', 'before'); } function hook_at( $hook, $cb ) { invoke( $hook, $cb, 'add', 'at'); } function hook_after( $hook, $cb ) { invoke( $hook, $cb, 'add', 'after'); } function unhook_before( $hook, $cb ) { invoke( $hook, $cb, 'remove', 'before'); } function unhook_at( $hook, $cb ) { invoke( $hook, $cb, 'remove', 'at'); } function unhook_after( $hook, $cb ) { invoke( $hook, $cb, 'remove', 'after'); } //example function th1(&$args,&$result) { $result['func'][]='th1'; print "in th1\n"; } function th2(&$args,&$result) { $result['func'][]='th1'; print "in th2\n"; } function bth(&$args, &$result ) { $result['func'][]='bth'; print "in bth\n"; } function ath(&$args, &$result) { $result['func'][]='tah1'; print "in ath\n"; } //build program hook_at('example','th1'); hook_at('example','th2'); hook_before('example','bth'); hook_after('example','ath'); //execute print "\nreturned: " . var_export( invoke('example', $something ), true); ?>