Giant elseif chain, giant switch, or tiny switch with functions?
I have a 9000-line PHP file which consists of about 30 discrete areas, navigated to through $_POST
variables. So one might be ...
elseif (isset($_POST['view_user'])
|| isset($_POST['edit_user'])
|| isset($_POST['process_user_status']))
... and so on. I probably have around 75 points of entry to these thirty areas, all handled by long elseif isset chains like the above.
I've been thinking about changing this to something a little more sane. The ideas I've come up with so far:
1) Boil the posts down to a boolean, and use that in the elseif chain. So the above would be boiled down to elseif ($area_user)
, with $area_user
being set to true
if any of the $_POST
s above were set. But this isn't really addressing the complexity issue.
2) Use cases instead of elseif. So the above would become...
case (isset($_POST['view_user'])):
case (isset($_POST['edit_user'])):
case (isset($_POST['process_user_status'])):
do stuff;
break;
But, again, while it removes the elseif syntax, it's just replacing it with something that, while slightly more manageable, might still be hiding the true problem.
3) Use functions. So at the top of the page, I have a similar switch statement, but instead of it being in the middle of the page going directly into the script area, it calls a function, so instead of 'do stuff' it might call UserArea($_POST[开发者_开发百科'whatever'])
. This has the advantage of moving all $_POST
variables outside of the meat of the script and concentrating them into the navigation and function calls. However, it will require lots of global function declarations that currently I don't need to do because the branches of the elseif are in the global scope.
4) Refactor entirely with a full MVC split, templates, etc. Would love to but not an option at this moment. Just be happy I have the model split off, but the view and controller have to coexist for now.
As I've been writing this I'm convincing myself more and more of 3, but I wanted to see what you kind folks thought. What should be the best practice navigation for a situation like this?
One more option for you: A dictionary of handler functions.
Put all your potential $_POST keys ('view_user' etc.) in an associative array pointing to the function (name) that handles them. Then, in place of your ifelse chain, iterate through the actual $_POST keys until you find a match in the array and call the associated function.
Now you can move your handler functions into other files without pain, though you may need to add some parameters to that dynamic function call. This kind of dispatch is used by most MVC frameworks (against patterns in the path, rather than form or querystring data).
// Untested, but something like this
$handlers = array(
'view_user' => 'UserArea',
'edit_user' => 'UserArea',
'view_document' => 'DocManagement', // for example
// etc...
);
foreach ($_POST as $key => $value) {
if(array_key_exists($key, $handlers)){
call_user_func($handlers[$key]);
}
}
精彩评论