PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > array_map : to call a method on all objects
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
array_map : to call a method on all objects

Réponse
 
LinkBack Outils de la discussion
Vieux 17/06/2008, 10h07   #1
DigitalBase
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 ?
  Réponse avec citation
Vieux 17/06/2008, 10h22   #2
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: array_map : to call a method on all objects

On Tue, 17 Jun 2008 11:07:07 +0200, DigitalBase <info@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
  Réponse avec citation
Vieux 17/06/2008, 10h32   #3
Dikkie Dik
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: array_map : to call a method on all objects

> 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.
  Réponse avec citation
Vieux 17/06/2008, 10h33   #4
Alexey Kulentsov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: array_map : to call a method on all objects

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);
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 14h03.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14141 seconds with 12 queries