开发者

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

Jai


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);

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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜