constructing class
| From: | Lorderon | Date: | Sun, 28 Nov 2004 00:13:38 +0000 |
| Subject: | constructing class | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-203228@lists.php.net to get a copy of this message | ||
Hi,
I want to create an instance of a class and construct it with different
params. The class name is a variable and the params change in each class. Is
there a way to pass params to the constructor of a class like you pass
params in the function call_user_func_array() ?
BTW, there's a solution for it using eval, but looking for a better cleaner
way...
With eval:
$cls="MyClass";
$construct_params=array(10,true);
$eval_str="return new $cls(";
for ($i=0; $i<count($construct_params); $i++)
$eval_str.="\$construct_params[$i],";
$eval_str=substr($eval_str,-1).");";
$obj=eval($eval_str);
thanks in advance,
Lorderon.