删除 PHP 中重复的硬编码循环和条件
我看到这个问题是关于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这段代码也许可以被显着地清理,并朝着 OO 的方向推进,而无需触及 SPL。
仅当您想要改变语言结构(如 foreach())、内置函数(如 count())或数组访问函数(运算符 [] 和函数(如 key()、next() 等))中的正常对象行为时,才需要 SPL 。
清理建议:
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:
如果我是你,我会从编写测试代码开始。
如果您可以构建一组完整描述您正在重构的功能的测试用例,您就可以继续安全地重写您的代码,因为您知道它仍然可以工作。
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.
如果我理解正确的话,你有 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.)
慢慢开始。 一次重构它一部分。
如果您要循环访问大量数组,请查看数组函数,例如
array_map
、array_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.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:
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.