I have a factory pattern like this:
public function ViewFactory implements Factory {
public function __construct() {
}
public static function Create($params) {
//does not return variables, only extracts them
$p = extract($params, EXTR_PREFIX_ALL, "var_");
//return object of view and pass in all variables extracted from array
return new View($p);
}
***
***
}
interface Factory {
public function Create($params);
***
***
}
Im trying to use extract but it does not return variables I just have to access them using keys from associative array prefixed by var_. Is it possible to somehow return all values of array as variables comma separated and pass it into function?
My View Class:
class View {
public function __construct($path, $parameters, $site_title) {
***
};
}
I'm not quite sure if this is what you are asking for, but you can use ReflectionClass::newInstanceArgs to create an instance of a class and pass it arguments from an array :
public static function Create($params) {
$class = new ReflectionClass('View');
return $class->newInstanceArgs($params);
}
You can just pass the three of them to the view like this :
// This will reset the keys in the array, so the keys will now be [0] [1] and [2]
$p = array_values($p);
// Pass the values one by one
return new View($p[0], $p[1], $p[2]);