php - How to pass a set of variables to a class -
this may bit of xy questions, i'm going explain i'm trying first. i'm attempting create single php file handle of page refresh ajax calls. means want able send class name, plus list of variables class constructor takes, , create class.
i can create class fine. $class = new $classname();
works fine creating class. problem passing in default variables. of variables objects containing other classes, can't include once class created, need pass them class created.
i thinking along lines of:
$varstr = ''; $s = ''; foreach($vars $var) { switch($var['type']) { case 'object': $varstr .= $s . '$' . $var['value']; break; case 'variable': $varstr .= $s . $var['value']; } $s = ','; } $class = new $classname(echo $varstr);
now echo $varstr isn't going work there, have no idea will. there can output variables array class constructor that? i'm trying possible? there better way?
whilst understand pass whole array class constructor, complicate main part of program, , rather ditch idea of single page ajax refresh go down route.
this wild guess @ you're trying maybe you're after:
// generate constructor args $args = array(); foreach($vars $var) { switch($var['type']) { $value = $var['value']; case 'object': args[] = ${$value}; // evaluate, think that's want? break; case 'variable': args[] = $value; // use break; } } // instanciate class args $class = new reflectionclass($classname); $obj = $class->newinstanceargs($args);
for work, require $vars enumerates args in correct order expected each class constructor.
Comments
Post a Comment