2011-01-06 4 views
0

I PHP에서 다음 객체 생성 라인이 있습니다객체를 생성하기 위해 문자열을 사용

$countryTargetSearchParameter = new CountryTargetSearchParameter(array(new CountryTarget('JP'))); 

내가로 대체하려고를 :

$countryTargetSearchParameter = new CountryTargetSearchParameter(array($LocArray)); 
$ LocArray 새로운 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'); 
+0

. 스티븐 감사합니다. 내가 찾던 대답은 매우 간단했습니다. 방금 eval 함수를 사용해야했습니다. – Jai

+0

eval을 사용하지 않는 것이 좋습니다. 내 방법을 시도 해 봤니? – Stephen

+0

스티븐 감사합니다. 당신의 방법도 효과가있었습니다. – Jai

관련 문제