2016-05-14 62 views
0

我需要發送和獲取類到另一個類像在MVC方法的變量:如何發送和提取

class Demo { 

private $vars = array(); 

function set($var , $data) { 
    $this->vars[$var] = $data; 
} 


function request() { 

    extract($this->vars); 

     var_dump($vars); 

    } 

} 

,我想用上面的類成這樣:

class Test extends foo { 

    function __construct(){ 
     $this->demo = new Demo(); 
    } 

    function register(){ 
     $user = Load::model('user');//Now User Is An Object 
     $this->demo->set('user',"$user");//This Is Error 
     $this->demo->request(); 
} 

Catchable fatal error: Object of class user could not be converted to string in

+0

是結束'}'在類'Test'實際的代碼? – sjsam

回答

-1

刪除引號

$this->demo->set('user', $user); 

而且不要忘了在你的類範圍

class Test extends foo { 
    public $demo; 

創建您的變量應該做的伎倆

+0

請給出一個有利於該行動的解釋,以便它對一般觀衆有用。 – sjsam

-1
class Test extends foo { 
     protected $demo; // you missed to declare this variable  

     function __construct(){ 
      $this->demo = new Demo(); 
     } 

     function register() { 
      $user = Load::model('user');//Now User Is An Object 
      $this->demo->set('user', $user); 
      $this->demo->request(); 
     } 
} 
+0

請說明一下。 – sjsam