php - Initiate a class instance with dynamic parameters? -
this question has answer here:
i'm trying make new class instance dynamic parameters e.g.:
$controller = 'classname'; $parameters = array('hello', 'world'); new $controller($parameters);
except parameters passed class naturally, if called method via call_user_func_array();
, want avoid having pass parameters in array.
is there way initiate class while calling call_user_func_array();
on constructor? or prevent automatic running of constructor can run via call_user_func_array();
properties array?
my actual code right is:
if(!class_exists($controller)) require('controllers/' . $controller . '.php'); return new $controller();
i pass array of parameters constructor $controller($parameters)
, in single array, not individual parameters.
the full file source can viewed in context here: https://github.com/pyrokinetic/mfw/blob/master/libraries/load.lib.php
$reflect = new reflectionclass($class); $instance = $reflect->newinstanceargs($args);
Comments
Post a Comment