删除 PHP 中重复的硬编码循环和条件

发布于 2024-07-06 15:37:39 字数 128 浏览 9 评论 0原文

我看到这个问题是关于 C# 的,我想要 PHP 的答案。 我有一些旧代码,有 4 页的 foreach 循环和条件,这使得阅读和理解变得困难。 我怎样才能让这个更加面向对象呢? 我正在考虑使用 SPL 函数,但尚未完全理解所涉及的内容。

I saw this question asked about C# I would like an answer for PHP. I have some old code that has 4 pages of foreach loops and conditions which just makes it hard to read and follow. How would I make this more OO? I was thinking of using SPL Functions but don't fully understand whats involved yet.

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

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

发布评论

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

评论(5

故笙诉离歌 2024-07-13 15:37:39

这段代码也许可以被显着地清理,并朝着 OO 的方向推进,而无需触及 SPL。

仅当您想要改变语言结构(如 foreach())、内置函数(如 count())或数组访问函数(运算符 [] 和函数(如 key()、next() 等))中的正常对象行为时,才需要 SPL 。

清理建议:

  • 如果同一操作在代码中执行多次(超过 1 或 2 次),请将其分解为一个函数。 如果要对数组中的所有元素执行此操作,请考虑使用 array_walk。 如果由于某种原因遍历数组不适合,请使用循环:)
  • 如果您有一些比键:值对更复杂的语义连接数据的多个实例,以及关联的操作,请考虑将其包装在一个类中。 但有正式记录的关联。 数组可能也适合你。 取决于您的风格和数据类型。 这里重要的是处理数据的方法的结构和文档。 你是否把它作为一堂课并不重要。
  • 完成上述步骤后,将新编写的函数和对象分解为单独的包含文件。 我倾向于将大部分内容都包含在一个类中,所以我主要有一个类的公关。 文件。 但有些辅助类与主类共享一个文件,等等。您会感觉到的。

This code can probably be cleaned up significantly, and pushed far in the direction of OO, without touching SPL.

SPL is only needed if you want to alter normal object behaviour in language constructs like foreach(), or in builtins like count(), or in array access funcitons (both operator [] and functions like key(), next() etc).

Cleanup suggestions:

  • If the same action is performed several (more than 1 or 2) times in the code, break it out into a function. If this action is to be performed on all elements in a array, consider using array_walk. If walking the array doesn't fit for some reason, use a loop :)
  • If you have several instances of some semantically connected data more complex than a key:value pair, with associated operations, consider wrapping it in a class. But a formally documented assoc. array might suit you just as well. Depends on your style, and on the type of data. The important thing here is the structuring of methods working on th data, and the documentation. Doesn't really matter if you make it a class or not.
  • After you have done the above steps, break the newly written functions and objects out into separate include files. I tend towards wrapping most everything in a class, so I mostly have one class pr. file. But some helper classes share a file with the main class, etc. You'll get a feel for it.
涙—继续流 2024-07-13 15:37:39

如果我是你,我会从编写测试代码开始。

如果您可以构建一组完整描述您正在重构的功能的测试用例,您就可以继续安全地重写您的代码,因为您知道它仍然可以工作。

PHPUnit 可能是一个很好的起点。

If I were you, I'd start by writing test code.

If you can build up a set of testcases that fully describe the functionality you're refactoring, you can go ahead and rewrite your code safe in the knowledge that it will still work.

PHPUnit might be a good place to start with this.

飘过的浮云 2024-07-13 15:37:39

如果我理解正确的话,你有 foreach 循环(等等)嵌套了 4 页深,并且你想知道你听说过的这个 OO 东西是否有帮助。

您当然应该继续重构代码以减少嵌套级别并提高可读性,但不要将其与面向对象混淆。

OO 是一种构建代码的方法,将数据结构的定义放在操作这些数据结构的代码旁边。 虽然其主要目标之一是通过提供复杂性的封装来提高可读性,但面向对象并不是实现这一目标的唯一方法。

如果您还不了解 OO 的概念,您可能会发现更容易重构代码,将内部循环内的代码分成单独的函数,(希望)每个函数都有一个简单的任务。

别误会我的意思; 我是面向对象的拥护者,尤其是作为一种为开发人员提供更高层次概念以更有效地讨论设计的技术。 OO非常值得学习。

但是,不要因为缺乏面向对象的知识而阻止您将内部代码从循环中取出并将它们放入单一用途的函数中。

(如果我误解了你的 OO 知识水平,我很抱歉。)

If I understand you correctly, you have foreach loops (and the like) nested 4 pages deep, and you are wondering if this OO thing you've heard about can help.

You should certainly go ahead and refactor your code to reduce the levels of nesting, and improve the readability, but don't confuse that with Object Orientation.

OO is a way of structuring your code to put the definition of the data structures next to the code that manipulates those data structures. While one of its key goals is to aid readability by providing encapsulations of complexity, OO isn't the only way to do it.

If you don't yet understand the concepts of OO, you may find it easier to refactor your code to separate the code inside the inner loops into separate functions which (hopefully) will each have a single, simple task.

Don't get me wrong; I am an advocate of OO, especially as a technique to provide developers with higher-level concepts to discuss design more efficiently. OO is well worth learning.

But don't let a lack of knowledge about OO stop you from pulling the inner code out of the loops and putting them into single-purpose functions.

(If I have misunderstood your level of OO knowledge, my apologies.)

紫罗兰の梦幻 2024-07-13 15:37:39

慢慢开始。 一次重构它一部分。

如果您要循环访问大量数组,请查看数组函数,例如 array_maparray_walk 和朋友们。 这更像是一种功能性重构,而不是面向对象的重构,但它会让你走得更远。 如果更多地面向对象是有意义的,那么您将拥有一些函数,然后可以根据需要将它们推送到适当的类中。

Start slowly. refactor it a piece at a time.

If you are looping over a lot of arrays, look at the array functions like array_map, array_walk, and friends. This is more a functional refactoring then an OO refactoring, but it would carry you pretty far. If it made sense to go more OO, then you will have some functions that you could then push into the proper classes as needed.

风筝有风,海豚有海 2024-07-13 15:37:39

Johnathan 和 gnud 提供了一些很好的建议。 不用太担心 SPL,了解有关重构的更多信息,并了解可用于 PHP 的重构工具

另外,我强烈推荐阅读有效地使用旧代码

替代文本

当然还有规范的重构书

替代文本 http://ecx .images-amazon.com/images/I/519XT0DER6L._SL500_BO2,204,203,200_AA219_PIsitb-sticker-dp-arrow,TopRight,-24,-23_SH20_OU01_.jpg

PHP 是一种很难清除代码异味的语言,但是只要有一点毅力就可以做到! 你会感谢自己每天都必须查看你的代码库。

Some good advice from Johnathan and gnud. Don't worry so much about the SPL, and learn more about refactoring, and look into what refactoring tools are available for PHP.

Also I can't recommend enough reading Working Effectively with Legacy Code:

alt text

And of course the canonical Refactoring Book:

alt text http://ecx.images-amazon.com/images/I/519XT0DER6L._SL500_BO2,204,203,200_AA219_PIsitb-sticker-dp-arrow,TopRight,-24,-23_SH20_OU01_.jpg

PHP is a difficult language to keep clean of code smells, but with a little perseverance it can be done! And you will thank yourself every day you have to look at your codebase.

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