use a string to create an object
I have the following object creation line in PHP:
$countryTargetSearc开发者_运维百科hParameter = new CountryTargetSearchParameter(array(new CountryTarget('JP')));
I am trying to replace it with:
$countryTargetSearchParameter = new CountryTargetSearchParameter(array($LocArray));
where $LocArray is equal to new CountryTarget('JP'). However, I am not able to get this to work. Can I get some help please.
Thank you
Jaiclass CountryTargetSearchParameter {
public function __construct(array $array_with_string) {
$this->new_object = new CountryTarget($array_with_string[0]);
}
}
$array_with_string = array('JP');
$test = new CountryTargetSearchParameter($array_with_string);
Or, much cleaner, and using a string for the class:
class CountryTargetSearchParameter {
public function __construct($name_of_class, $parameter) {
$this->new_object = new $name_of_class($parameter);
}
}
$test = new CountryTargetSearchParameter('CountryTarget', 'JP');
精彩评论