2012-11-28 1 views
0

메서드 내부에서 인스턴스 변수를 설정하고 변수 이름을 해당 값으로 설정하는 방법은 무엇입니까? 설명은 코드입니다. 1에서 4까지의 단계를보세요.메서드 내부에서 인스턴스 변수를 설정하고 변수 이름을 해당 값과 동일하게 설정하는 방법

$$variable을 사용할 수 있지만 새로운 이름으로 인스턴스 변수를 만드는 방법은 알고 있습니까?

class Control 
{ 
    //2. i pass the name of class and make a new object 
    function model($object) 
    { 
    // 3. I create here variable with the name i set. so now it should be 
    // accessible as $HomeModel but how to make it accessible for other methods. 

    $$object=new $object(); 
    } 
} 

class HomeController extends Control 
{ 
    //THIS IS START 
    public function __construct() 
    { 
    // 1. when i set the name here ['HomeModel'] and run class method model 
    $this->model('HomeModel'); 

    //4. and use it ie. here 
    $data=$this->HomeModel->getData(); 
    } 
} 

class HomeModel 
{ 
    public function getData() 
    { 
    echo "data"; 
    } 
} 

$x = new HomeController(); 

답변

0

가 있어야한다 : 당신

class Control 
{ 
    //2. i pass the name of class and make a new object 
    function model($object) 
    { 
    // 3. i want to create here an instance variable with the name i set 
    $this->{$object} = new $object(); 
    } 
} 
+0

많은 감사 - 작동 :) –

관련 문제