Thursday, September 22, 2011

How to instantiate a PHP class with dynamic parameters

Two Best Solutions :

function create_instance($class, $params) {

return call_user_func_array(
array(new ReflectionClass($class), 'newInstance'),
$params
);
}

or

function create_instance($class, $params) {

$reflection_class = new ReflectionClass($class);
return $reflection_class->newInstanceArgs($params);
}

No comments:

Post a Comment