PHP 数组映射和项目类的方法


PHP arraym map and method of item class

我有数组:

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

如何使用数组映射为数组中的每个项目调用 SomeClass 类的方法(非静态)?

有一种比array_maparray_walk更易读的方法:

$instances = array(
  0 => new SomeClass(1),
  1 => new SomeClass(2),
  2 => new SomeClass(3),
)
foreach($instances as $instance)
{
    $instance->foo();
}

但如果你真的想要array_map

array_map(function($instance) {
    $instance->foo();
}, $instances);