Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.)

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top