I have a piece of code that basically creates an instance of a class on-the-fly:
$class = 'user';
return new $class;
The challenge is that I have the constructor's arguments in an array format and they're obviously different from class to class -- so there is no pattern at all that I can make anything on top of that. There are some workarounds here to extract these arguments from the array and pass it all through in a way like this:
$args = array( 'one', 'two' );
// some code to generate $arg1, $arg2, etc.
return new $class( $arg1, $arg2 );
However, I really prefer to do it in a smarter way. As far as I can see there is no way to make use of call_user_func and call_user_func_array unless I forget about the constructor and initialize it after instantiation.
Update
Basically I'm looking for something like this:
return magic_func( 'className', array('one', 'two') );