On 22 Feb 2003 03:28:22 -0000, you wrote:

>Let's say I need to take one of 20 actions depending on a form selection. I 
>could use a switch statement with 20 cases, but I could also do something 
>like:
>
>// Pretend this comes from a form
>$formchoice = "mars";
>
>
>$response = array(
> "mars" => "go_mars()",
> "mercury" => "go_mercury()",
> "earth" => "go_earth()");
>
>foreach ($response as $choice => $action)
>{
> if (strtoupper($formchoice) == strtoupper($choice))
>  {
>  eval("$action;");
>  }
>}
>       
>But are there even better ways? 

$formchoice = "mars";

go_planet ($formchoice);

Keep the high-levels of the script as simple as possible, push the
complexity down into functions where it's hidden from view.

Maybe go_planet() just calls go_mercury(), go_venus(), etc., or, more
likely,  there's enough shared-functionality for only go_planet() to be
necessary.

Either way, you've replaced 11 lines in the main body of your script
with 1 line, and the next person to deal with your code will be
grateful.

[An OO programmer would probably create a planet object...

myPlanet = new Planet()
myPlanet.setType($formchoice)

or something, with the same end (hiding complexity) in mind.]


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to