Skip navigation.
Home
That which cannot be rendered in binary is by definition a delusion
 

OOP Case Handling: From Switch to Event Management

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

  • Allowed HTML tags: <a> <p> <span><small> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <embed> <object> <param> <strike> <caption>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options