陷入困境,需要帮助突破到新的水平

发布于 2024-09-12 02:12:19 字数 602 浏览 8 评论 0原文

我正在用我自学的平庸的 PHP 技能开发一个不起眼的网站,当前的界面结构是这样的:

<?php
  if (A) {
    $output = someFunc(A);
  } else if (B) {
    $output = anotherFunc(B);
  } else if (C) {
    $output = yetAnotherFunc(C);
  } else {
    $output = 'default stuff';
  }
?>
<html template top half>

<?php echo $output; ?>

</html template bottom half>

起初工作正常,看起来组织得很好,但所需的功能增长了 10 倍它正在迅速变成一个难以维护、令人尴尬的混乱,我不知道如何摆脱它。

我觉得每种情况下调用的函数都写得相当好并且重点突出,但我不知道如何处理用户与创建布局和处理返回的函数之间的中间步骤。

我有一种感觉 MVC 是一种解决方案?但我很难掌握如何从这里转到那里......

对于上述代码可能引发的任何头痛或不愉快的记忆,我深表歉意。感谢您抽出时间。

I'm working on a humble website with my mediocre, self-taught PHP skills, and the current interface structure is like this:

<?php
  if (A) {
    $output = someFunc(A);
  } else if (B) {
    $output = anotherFunc(B);
  } else if (C) {
    $output = yetAnotherFunc(C);
  } else {
    $output = 'default stuff';
  }
?>
<html template top half>

<?php echo $output; ?>

</html template bottom half>

This worked ok at first, and seemed pretty well organized, but the functionality required has grown by a factor of 10 and it's rapidly turning into an unmaintainable, embarrassing mess and I don't know how to get out of it.

I feel that the functions being called for each situation are fairly well-written and focused, but am at a loss as to how to handle the middle-step between the user and the functions that creates the layout and handles the return.

I have a feeling that MVC is one solution? But I'm having a hard time grasping how to go from here to there...

I apologize for any headaches or unpleasant memories the above code may have prompted. Thank you for your time.

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

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

发布评论

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

评论(3

树深时见影 2024-09-19 02:12:19

你似乎已经像很多人一样开始了,一个不断增长的大的“如果”和/或“案例”陈述。所有这些“如果”检查都需要时间。 MVC 绝对是一个很好的方法,但是实现它的方法有很多。我还建议您查看前端控制器设计模式,该模式通常与 MVC 一起使用。

改变工作方式的一种方法是使用关联数组切换到定义的“操作”列表。并将您的功能更改为包含。然后你可以有多个函数、变量和其他处理代码。

$actions = array(
'A'=>'action1.php',
'B'=>'action2.php',
'C'=>'action3.php',
'default'=>'action_default.php'
);
if ( isset( $actions[ $_GET['action'] ] ) ) {
    include( $actions[ $_GET['action'] ] );
} else {
    include( $actions['default'] );
}

那么你的“索引”文件只是一个路由工具,这几乎是前端控制器的概念。

You seem to have started the way a lot of people do, a big if and/or case statement that continues to grow. All those "if" checks can take time. MVC is definitely a great way to go, but there are so many ways to implement it. I would recommend also looking to the Front Controller design pattern, which is commonly used with MVC.

One way to change how you are doing things is to switch to a defined list of "actions" using an associative array. And change your functions into includes. Then you can have multiple functions, variables and other processing code.

$actions = array(
'A'=>'action1.php',
'B'=>'action2.php',
'C'=>'action3.php',
'default'=>'action_default.php'
);
if ( isset( $actions[ $_GET['action'] ] ) ) {
    include( $actions[ $_GET['action'] ] );
} else {
    include( $actions['default'] );
}

Then your "index" file is just a routing tool, which is pretty much the Front Controller concept.

爱给你人给你 2024-09-19 02:12:19

在任何面向对象的代码中,每当您看到一系列 if/else 或 case 语句时,通常最好通过对象层次结构来处理它,而不是一堆 if/else/else。

在您的特定情况下,您可能会为 A、B 和 C 使用 3 个不同的类。并且只需调用一个 $obj->doit() 方法。 A、B 和 C 各有不同的 doit() 方法实现。

如果你想打破常规,我建议你阅读一本关于设计模式的书。以下是 PHP 中设计模式的一些示例: http://www.fluffycat.com/PHP -Design-Patterns/

对于这种特定类型的问题,您可能特别感兴趣的模式是:

  • 策略。
  • 命令。
  • 工厂。

上面的链接中包含 PHP 示例对这些模式的描述。我确实建议在其他地方阅读更多有关设计模式的内容,但该链接有 PHP 示例。

实际上,我通常会在与您所描述的情况类似或相同的情况下一起使用其中的三个来清晰地组织代码。它的工作原理如下:
- 战略。这就像千篇一律或疯狂自由风格的事情。您设置了执行功能所涉及的基础知识。用户(使用基类的程序员)可以根据需要重写某些内容,但完成工作的基础知识都已就位。示例:我们需要执行一个业务流程。通常,这涉及启动事务、完成工作的“主要内容”,然后执行清理和日志记录。在这里使用策略/千篇一律的模式似乎有些过分,但通常情况并非如此:即使代码的“核心”中抛出了错误或异常,您也需要记住进行清理。通过使用策略/千篇一律的模式,您可以用最少的样板/重复代码实现这一切。

  • 命令:结合我上面提到的策略模式的思想,您可以在执行实际工作的函数中减少样板代码。

  • 工厂。您可以使用工厂方法来针对您所面临的情况生成适当类型的命令。

这 3 种设计模式组合在一起非常适合这种情况,并且可以生成一些非常干净的代码,让您永远不会重复自己。

下面的maschka推荐使用Smarty,这是一个合理的想法。根据您的应用程序,更好的想法可能是使用 MVC 框架。如果您决心坚持使用 PHP,那么我推荐 CakePHP,并且已经取得了巨大的成功。它可以帮助您以非常好的方式布局代码并帮助您摆脱麻烦。

In any object oriented code, whenever you see a series of if/else or case statements, it's often better handled by an object hierarchy rather than a bunch if if/else/else.

In your particular case, you might instead have 3 different classes for A, B, and C. And just call a single $obj->doit() method. A, B, and C would each have different implementations of the doit() method.

If you want to get out of your rut, I recommend reading a book about design patterns. Here are some examples of design patterns in PHP: http://www.fluffycat.com/PHP-Design-Patterns/

Patterns that may be of particular interest to you for this particular type of problem are:

  • Strategy.
  • Command.
  • Factory.

Descriptions of these patterns with PHP examples are in the above link. I do recommend reading up more about design patterns elsewhere, but that link has PHP examples.

I actually routinely use the 3 of those together to cleanly organize code in situations similar or identical to what you are describing. It works like this:
- Strategy. This is like a cookie-cutter or Mad Libs style of thing. You set up the basics involved in performing a function. The user (the programmer using the base class) can override certain things if he needs to, but the basics for doing the work are all in place. Example: we need to perform a business process. Generally that involves starting a transaction, doing the "meat" of the work, then performing cleanup and logging. Seems overkill perhaps to use a Stragegy/cookie-cutter pattern here, but it's often not: you need to remember to do the cleanup even if there was an error or exception thrown in the "meat" of the code. By using the Strategy/cookie-cutter pattern, you can make all that happen with minimal boilerplate/repeated code.

  • Command: Combined with the ideas of the Strategy pattern I mentioned above, you have less boilerplate code in your functions that do the actual work.

  • Factory. You use a factory method to generate the appropriate kind of command for the situation you're faced with.

Those 3 design patterns put together work very well for this kind of situation, and can lead to some very clean code where you never repeat yourself.

maschka below recommended using Smarty, which is a reasonable idea. A better idea, depending on your application, may be to use an MVC framework. If you're committed to the idea of sticking with PHP, then I recommend CakePHP and have had great success with it. It helps you to lay out your code in a very good way and help keep you out of trouble.

被翻牌 2024-09-19 02:12:19

一般建议:您是否看过Smarty?我发现它是一个相当有用的模板引擎。这可能需要一些学习,但从长远来看可能是值得的。

A general suggestion: Have you had a look at Smarty? I found it to be a fairly useful template engine. It probably requires some learning but it may be worth it in the long run.

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