我在PHP中有以下对象创建行:
$countryTargetSearchParameter = new CountryTargetSearchParameter(array(new CountryTarget('JP')));
我正在尝试将其替换为:
$countryTargetSearchParameter = new CountryTargetSearchParameter(array($LocArray));
其中$ LocArray等于new CountryTarget('JP')。但是,我无法让这个工作。我可以帮忙吗。
谢谢你 洁
答案 0 :(得分:0)
class 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);
或者,更清洁,并为班级使用字符串:
class CountryTargetSearchParameter {
public function __construct($name_of_class, $parameter) {
$this->new_object = new $name_of_class($parameter);
}
}
$test = new CountryTargetSearchParameter('CountryTarget', 'JP');