Page 1 of 1

Array of objects, generic composite pattern

Posted: Mon Oct 30, 2006 4:58 pm
by johno
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.

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?
Then a breakthrough moment came to me and I realized that this could be a really nice place for __call method.

How about?

Code: Select all

$items->callSomeMethod();
// or
$buffer = $items->anotherMethod($parameters);
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.

Posted: Mon Oct 30, 2006 6:25 pm
by Ollie Saunders
Not so much a pattern more an array object. Very nicely written too. I've written something similar before.
Side-tip: Use self for the name of the current class wherever you can.

Posted: Tue Oct 31, 2006 4:09 am
by johno
ole wrote:Not so much a pattern more an array object.
Actually I think its exactly the composite pattern. http://en.wikipedia.org/wiki/Composite_ ... #Structure You can add, remove components and call methods on composites which call these methods on all components they contain.
ole wrote:Very nicely written too. I've written something similar before.
Thank you!
ole wrote:Side-tip: Use self for the name of the current class wherever you can.
Hmm, what did you mean by this? I don't really understand.

Posted: Tue Oct 31, 2006 5:39 am
by Maugrim_The_Reaper
Looks like the Composite Pattern...

I believe Ole is referring to the fact that the PHP4 use of $this can often be replaced in PHP5 using "self", just as a parent class can be access using "parent". I usually use "self" when calling statics/constants or creating a new object of the same type as the host... Nothing technically wrong with using $this in many cases - maybe Ole can illustrate his observation???

Posted: Tue Oct 31, 2006 6:25 am
by johno
Maugrim_The_Reaper wrote:I usually use "self" when calling statics/constants or creating a new object of the same type as the host... Nothing technically wrong with using $this in many cases - maybe Ole can illustrate his observation???
Now I know. He was actually referering to this

Code: Select all

protected function createArray() {
    return new ArrayObject();
}
which can be written as

Code: Select all

protected function createArray() {
    return new self();
}
Thanks!

Posted: Tue Oct 31, 2006 12:53 pm
by Ollie Saunders
johno, yep that's it.