470,848 Members | 1,290 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,848 developers. It's quick & easy.

array_map : to call a method on all objects

Hi,

I am trying to call a method on all objects in an array.

What i am doing now ::

$groups = $user->getGroups();
$newarray = array();
foreach ($groups as $group) {
$newarray[] = $group->getId();
}

I want to use a core feature of php to achieve this something like :

$groups = $user->getGroups();
array_map(array( ???? ,"getId"),$groups);

Using the callback (first argument) i can call static methods, normal
functions but i can not call methods of the object itself.

I am not trying to start a discussion about performance, lazyness vs
clean code etc. The point is, i have about 30 different methods that
use that foreach() to call an object method, and everytime i write a
foreach like this i am thinking there should be a better way...

Anyone ?
Jun 27 '08 #1
3 4808
On Tue, 17 Jun 2008 11:07:07 +0200, DigitalBase <in**@digitalbase.eu
wrote:
Hi,

I am trying to call a method on all objects in an array.

What i am doing now ::

$groups = $user->getGroups();
$newarray = array();
foreach ($groups as $group) {
$newarray[] = $group->getId();
}

I want to use a core feature of php to achieve this something like :

$groups = $user->getGroups();
array_map(array( ???? ,"getId"),$groups);

Using the callback (first argument) i can call static methods, normal
functions but i can not call methods of the object itself.

I am not trying to start a discussion about performance, lazyness vs
clean code etc. The point is, i have about 30 different methods that
use that foreach() to call an object method, and everytime i write a
foreach like this i am thinking there should be a better way...
Instead of a vanilla array, you could try to use an
ArrayObject/ArrayIterator, and define your own cascade of methods for
those. Alternatively, you could use a Composite pattern instead of an
array.
--
Rik Wasmus
....spamrun finished
Jun 27 '08 #2
I am not trying to start a discussion about performance, lazyness vs
clean code etc. The point is, i have about 30 different methods that
use that foreach() to call an object method, and everytime i write a
foreach like this i am thinking there should be a better way...

Anyone ?
I think you would like the Visitor pattern.
Jun 27 '08 #3
DigitalBase write:
Hi,

I am trying to call a method on all objects in an array.

What i am doing now ::

$groups = $user->getGroups();
$newarray = array();
foreach ($groups as $group) {
$newarray[] = $group->getId();
}

I want to use a core feature of php to achieve this something like :

$groups = $user->getGroups();
array_map(array( ???? ,"getId"),$groups);
$idList=array_map(create_function('$group','return
$group->getId();'),$groups);
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Reply-Via-Newsgroup | last post: by
8 posts views Thread by Lucas Lemmens | last post: by
6 posts views Thread by HolyShea | last post: by
44 posts views Thread by Steven D'Aprano | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.