OOP Case Handling: From Switch to Event Management
Submitted by bingomanatee on 6 December, 2009 - 12:49
A debate on the SF PHP meetup boards got into what the "best" way to use switches (if at all) was with examples of waterfalling etc. The code below attempts to emulate case behavior with an OOP suite of classes.
I regard the case structure as a kind of compact syntactic event handler; each case is a response triggered by the event it expects. however because of the "waterfall" behavior -- that is, the fact that you have the option of NOT including a "break" closure, you are NOT locked into the singular response that an (if/then/elseif)+ structure implies; that is one OR MORE events may fire off in response to the case key.
Here is the premise in spec form.
- Event handlers (cases) are in a linear stack and respond to events in order.
- Unlike a switch case, a given handler can respond to one OR MORE named events.
- When an event occurs, the handlers are tested in order against the event name.
- Each event MAY OR MAY NOT execute in response to a given event.
- Each event handler, if it executes, MAY OR MAY NOT terminate the event upon execution, preventing events below itself in the stack from being tested.
- You may (should) have a default "cleanup" event that ALWAYS fires off when all others fail to terminate the event.

Post new comment