2013-10-07 86 views
1

我有下面的代碼,我試圖找到一個解決方案:PHP變量委託功能

<?php 

class t1 { 

    public $response = array(); 
    public $request = array(); 

    public function getRequest() { 
     return $this->request; 
    } 

    public function getResponse() { 
     return $this->response; 
    } 

} 

class t2 extends t1 { 
    public function run($f) { 
     $this->response = $f($this); 
     return $this; 
    } 
} 

$delegate = function($c) 
{ 
    // PLACEHOLDER 
    // This is the only place to update test code 
    // It's not allowed to modify any other section of this code 
}; 

$t = new t2(); 
print_r(array("request" => $t->run($delegate)->getRequest(), "response" => $t->getResponse())); 
?> 

我假設$代表是一個動態的功能。任何人都可以通過這個來引導我。

我想在佔位符應爲​​:

+2

你的問題是什麼? – ComFreek

回答

0

我假設$代表是一個動態的功能。

是的。 $delagate是一個PHP Closure

如果你定義功能:

$delegate = function($c) 
{ 

}; 

,並把它傳遞給$t->run,然後$c將是$t實例。