2010-03-24 20 views
1

我已經解決了這個問題,我只需要知道該怎麼做。我得到了上面的錯誤,因爲我剛剛意識到該類正在作爲class :: function($ values)而不是class-> function($ values)運行。

有誰知道如何將此函數轉換爲實例化類,然後運行帶有值的函數?

private function _load($values=null) { 

    define('LOADED_CONTROLLER', $this->controller); 
    define('LOADED_FUNCTION', $this->function); 

    $function = $this->function; 

    $controller = new $this->controller; 
    ($values == null) ? $controller->$function() : call_user_func_array(array($this->controller, $function), $values); 
} 
+0

你到底在問什麼?你說你知道問題是什麼,爲什麼你仍然試圖使用'$ this'來工作?你不能在靜態上下文中使用'$ this'。 – ryeguy 2010-03-24 16:28:54

回答

5

您已經實例化類:

$controller = new $this->controller; 

只要用你的$controllercall_user_func_array

($values == null) ? $controller->$function() : call_user_func_array(array($controller, $function), $values); 

在你的代碼試圖調用靜態方法的類,如果$value != null 。如果在此方法中使用$this,那當然會導致錯誤。

+0

對不起,我不明白。因爲我不知道使用call_user_func_array的參數數量。但是,當我這樣做時,我不能在我的控制器功能中使用$ this。有其他功能嗎? – JasonS 2010-03-24 16:30:18

+0

@JasonS:當然可以。但是,目前你正在向該函數傳遞類'array($ this-> controller,$ function)'而不是對象'array($ controller,$ funtion)'。那是你的錯誤所在。 – 2010-03-24 16:32:12

+0

現在啊,我無法弄清楚改變了什麼。非常感謝! – JasonS 2010-03-24 16:33:43

相關問題