0

I have array:

array(
  0 => new SomeClass(1),
  1 => new SomeClass(2),
  2 => new SomeClass(3),
)

How can I use array map to call method (non-static) of SomeClass class for each item in my array?

Mirgorod
  • 31,413
  • 18
  • 51
  • 63

1 Answers1

3

There's a more readable way than array_map or array_walk:

$instances = array(
  0 => new SomeClass(1),
  1 => new SomeClass(2),
  2 => new SomeClass(3),
)

foreach($instances as $instance)
{
    $instance->foo();
}

but if you really want array_map:

array_map(function($instance) {
    $instance->foo();
}, $instances);
Kris
  • 40,604
  • 9
  • 72
  • 101