- array_map: documentation ( source)
- var_dump: documentation ( source)
<?php
class User {
}
class UserProvider {
public function getServices() {
return ['user'];
}
public function user() {
return new User();
}
}
$provider = new UserProvider();
$services = array_map(function($factoryName) use($provider) {
return $provider->{$factoryName}(); // <-- dynamic call
}, $provider->getServices());
var_dump($services);