473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 4987
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Reply-Via-Newsgroup | last post by:
Folks, I have a multi-dimensional array that I read from my mysql database. I'd like to run strip slashes against each element and I'm pretty sure that array_walk() (or array_map) is likely to...
8
by: Lucas Lemmens | last post by:
Dear pythonians, I've been reading/thinking about the famous function call speedup trick where you use a function in the local context to represent a "remoter" function to speed up the 'function...
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
1
by: Jeffrey Kingsley | last post by:
I would like to cancel a call to BeginInvoke of a delegate (i.e. kill the thread the call was made on). This would be done when a timeout occures for the waitone call to the WaitHandle object of...
2
by: jw56578 | last post by:
Is it possible to call a method from a parent object, but have the childs overriden method called instead of the base method? I want several children objects to all call a certain method when they...
54
by: Zytan | last post by:
I have a log class that makes a synchronized TextWriter like so, in the constructor: StreamWriter sw = new StreamWriter(filename); tw = TextWriter.Synchronized(sw); In the destructor,...
6
by: HolyShea | last post by:
All, Not sure if this is possible or not - I've created a class which performs an asynchronous operation and provides notification when the operation is complete. I'd like the notification to be...
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.