2013-07-23 128 views
0

我目前正在重新編碼一些代碼。 這是我現在運行的項目。數組作爲函數參數

class store extends TModel{ 
    public function render_cart($a=0, $b=0, $c=0){ 
     echo '<pre>'; var_dump($a=0, $b=0, $c=0); echo '</pre>'; 
    } 
} 

class TController extends TObject{ 
    function getModel($model=''){ 
     include(TPATH_COMPONENT.'models'.DS.$model.'.php'); 
     $this->_model = new $model; 
     return false; 
    } 
    function get($method=''){ 
     $args = func_get_args(); 

     return $this->model->$method($args); 
    } 
} 

$controller->getModel('store');   
$cart = $controller->get('render_cart', 1, 2, 3); 

我想從TController轉移ARGS ::獲得存儲:: render_cart($ A = 0,$ B = 0,$ C = 0),因爲它的參數。謝謝你的幫助。

回答

2

你需要編寫的方法是這樣的:

function get($method=''){ 
    $args = func_get_args(); 
    $method = array_shift($args); 
    return call_user_func_array([$this->model, $method], $args); 
} 

獲取方法名和參數到$args後,使用array_shift方法名從參數隔離。

要將參數(其編號未知)傳遞給模型的方法,您需要使用call_user_func_array。你已經有了這個調用的參數數組,但你仍然需要callable。該文件是按照文檔中的兩個項目的數組在現場創建的。