巨型elseif链,巨型开关,还是带功能的小开关?

发布于 2024-10-18 05:06:18 字数 1130 浏览 3 评论 0原文

我有一个 9000 行的 PHP 文件,其中包含大约 30 个离散区域,通过 $_POST 变量导航到。所以一个可能是……

elseif (isset($_POST['view_user']) 
     || isset($_POST['edit_user']) 
     || isset($_POST['process_user_status']))

等等。我可能有大约 75 个进入这 30 个区域的点,所有这些都由像上面这样的长 elseif isset 链处理。

我一直在考虑将其更改为更理智的东西。到目前为止我提出的想法:

1)将帖子归结为布尔值,并在 elseif 链中使用它。因此,上面的内容可以归结为 elseif ($area_user),如果 $ 中的任何一个都将 $area_user 设置为 true上面的_POST已设置。但这并没有真正解决复杂性问题。

2)用用例代替elseif。所以上面的内容会变成……

case (isset($_POST['view_user'])):
case (isset($_POST['edit_user'])):
case (isset($_POST['process_user_status'])):
    do stuff;
    break;

但是,虽然它删除了 elseif 语法,但它只是用一些更易于管理的东西替换它,但可能仍然隐藏了真正的问题。

3)使用函数。因此,在页面顶部,我有一个类似的 switch 语句,但它不是在页面中间直接进入脚本区域,而是调用一个函数,因此它可能调用 UserArea($_POST['whatever']).这样做的优点是可以将所有 $_POST 变量移出脚本的核心部分,并将它们集中到导航和函数调用中。但是,它将需要大量全局函数声明,目前我不需要这样做,因为 elseif 的分支位于全局范围内。

4)使用完整的 MVC 拆分、模板等进行完全重构。很愿意,但目前不是一个选择。很高兴我将模型分开了,但视图和控制器现在必须共存。

当我写这篇文章时,我越来越说服自己相信 3,但我想看看你们的想法。对于这种情况,最佳导航实践应该是什么?

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 $_POSTs 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌︼了一个春 2024-10-25 05:06:18

您还有一个选择:处理函数字典。

将所有潜在 $_POST 键(“view_user”等)放入指向处理它们的函数(名称)的关联数组中。然后,代替 ifelse 链,迭代实际 $_POST 键,直到在数组中找到匹配项并调用关联的函数。

现在,您可以轻松地将处理程序函数移动到其他文件中,尽管您可能需要向该动态函数调用添加一些参数。大多数 MVC 框架都使用这种调度(针对路径中的模式,而不是表单或查询字符串数据)。

// 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]);
  }
}

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]);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文