Join The San Francisco PHP Meetup Group

You'll get invited to our Meetups as soon as they're scheduled!

Re: [php-139] NEW TOPIC - PHP6

From: Dave Edelhart
Sent on: Sunday, December 6, 2009 1:03 PM
http://wonderlandlabs.com/wll_drupal/os/switch_oop.html

is my switch solution.

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.
The classes in the above page satisfy and explain the even handler.  

While classic switch does manage a little ad-hoc event management it is constrained by its syntax and limited to consideration of a single parameter. Zupal_Switch responds to multiple conditions, allows event trapping and in other ways handles the behavior of switches. It manages the case handling into designated callbacks/functions which gives you a little more latitude and allows you to manage the response of the switch in remote resources/libraries. 

I am sure there are other systems in existence which do all this and more, but I always wanted to write a bit of event management code. :D

On Dec 1, 2009, at 5:20 PM, Michael wrote:

Are those two examples the same?  Where is do_de_stuff() in the if/else?  I think the switch should be easier to maintain when the application may add/delete cases, need a default, or change the procedures for any subset.  The source code size is about the same, but I am not sure about the bytecode.  Either way, the way to go is clearer to me when the $var values and their methods were semantically associated to the app.
 
If you use if/else, this would be clearer to me --
 
    if ('a' == $var || 'b' == $var) {
        do_b_stuff();
        do_abc_stuff();
    }
    elseif ($var == c) {
        do_abc_stuff();
    }
    elseif ('d' == $var) {
        do_d_stuff();
        do_de_stuff();
    }
    elseif ('e' == $var) {
        do_de_stuff();
    }    
    else {
        do_exception();
    } // end pseudoswitch
 
 
-------- Original Message --------
Subject: Re: [php-139] NEW TOPIC - PHP6
From: Joseph Engo <[address removed]>
Date: Tue, December 01, 2009 4:11 pm
To: [address removed]

This is a classic example of why I put the curly on the next line :P

On Dec 1, 2009, at 4:07 PM, Jesse Mullan wrote:

> Speaking of readability, that switch needs some comments.
>
> switch($var) {
> case 'a':
> /* Falls through */
> case 'b':
> do_b_stuff();
> /* Falls through */
> case 'c':
> do_abc_stuff();
> break;
> case 'd':
> do_d_stuff();
> /* Falls through */
> case 'e':
> do_de_stuff();
> break;
> }
>
>
> I dunno, which is more readable?
>
> if ('a' == $var || 'b' == $var || 'c' == $var) {
> if ('c' != $var) {
> do_b_stuff();
> }
> do_abc_stuff();
> } elseif ('d' == $var || 'e' == $var) {
> if ('d' == $var) {
> do_d_stuff();
> }
> do_e_stuff();
> }
>
>
> On Tue, Dec 1, 2009 at 3:04 PM, Dave Edelhart
> <[address removed]> wrote:
>
>> Cris does have a point in saying that there are readability issues
>> with the
>> switch Jesse posits. But you also cannot deny that the switch he
>> puts forth
>> does reflect a complex set of logical conditions in a very compact
>> unit of
>> code.
>> Readability is after all a reflection of what kind of code you
>> like/write/expect to be reading, and is therefore somewhat
>> subjective.
>
>
>
>
> --
> Please Note: If you hit "REPLY", your message will be sent to
> everyone on this mailing list ([address removed])
> /
> This message was sent by Jesse Mullan ([address removed]) from The
> San Francisco PHP Meetup Group.
> To learn more about Jesse Mullan, visit his/her member profile: /members/9265601/
> To unsubscribe or to update your mailing list settings, click here: http://www.meetup.com/account/comm/
> Meetup Inc. PO Box 4668 #37895 New York, New York 10163-4668 | [address removed]
>




--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
/
This message was sent by Joseph Engo ([address removed]) from The San Francisco PHP Meetup Group.
To learn more about Joseph Engo, visit his/her member profile: /members/8907494/
To unsubscribe or to update your mailing list settings, click here: http://www.meetup.com/account/comm/
Meetup Inc. PO Box 4668 #37895 New York, New York 10163-4668 | [address removed]





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Michael ([address removed]) from The San Francisco PHP Meetup Group.
To learn more about Michael, visit his/her member profile
To unsubscribe or to update your mailing list settings, click here

Meetup Inc. PO Box 4668 #37895 New York, New York 10163-4668 | [address removed]

Organized by
Asst. Organizers:

Our Sponsors

GameSpot.com

GameSpot.com & CBSi provides the facilities our group uses every month.

O'Reilly User Group Program

The O'Reilly User Group Program provides the great books we get monthly.