Name Based Dispatch

Here's a simple way to do name-based dispatch.


$dispatchTable = array(
'First Thing' => 'functionName',
'Second Way' => array('ClassName', 'StaticMethod')
);
...
...
$data = "Your data here, probably in an array.";
call_user_func( $dispatchTable[ $name ], $data );

This is nice, because you can put the dispatch table at the top of your code, or in a separate file. I'm using it in a payment processing system, where different products may be dispatched to different code. You get a level of indirection here, where you can swap out different methods for each product (or use the same method).

The only "trick" here is that, call_user_func() takes a pseudo-type known as a callback. Callbacks come in three forms:

string 'fooFunc' this will call fooFunc()
array array('Foo','func') this will call Foo::func()
array array( $foo, 'func') this will call $foo->func()