Array of objects, generic composite pattern
Posted: Mon Oct 30, 2006 4:58 pm
Hi,
I've been experimenting with some refactoring opportunities in my code and realized that pretty much I am using foreach loops just to iterate trough an array of objects and call some simple method on each of them.
Then a breakthrough moment came to me and I realized that this could be a really nice place for __call method.
How about?
Implementation was a matter of minutes. http://johno.jsmf.net/knowhow/sugar/
In fact it looks like some sort of generic composite pattern to me. So what do you think? Could this be useful for you? Any suggestions?
Please read this before posting: I know about Ruby and its blocks support. I don't want to hack PHP to support them. Don't let this be flame about Ruby vs PHP vs something else.
I've been experimenting with some refactoring opportunities in my code and realized that pretty much I am using foreach loops just to iterate trough an array of objects and call some simple method on each of them.
Code: Select all
// how about this
foreach($items as $item) {
$item->callSomeMethod();
}
// or this one
$buffer = array();
foreach($items as $key => $item) {
$buffer[$key] = $item->anotherMethod($parameters);
}
// do you recognize them in your code?How about?
Code: Select all
$items->callSomeMethod();
// or
$buffer = $items->anotherMethod($parameters);In fact it looks like some sort of generic composite pattern to me. So what do you think? Could this be useful for you? Any suggestions?
Please read this before posting: I know about Ruby and its blocks support. I don't want to hack PHP to support them. Don't let this be flame about Ruby vs PHP vs something else.