2016-04-02 10 views
-1

當我使用$thiscreate_function我得到一個錯誤

Fatal error: Using $this when not in object context

create_function('$model, $this', 'return $this->di->getStatus($model->getStatus)'); 

後來我這樣稱呼它

$function($model, $this); 

我通過$this到該功能,但似乎並沒有工作。有辦法解決它嗎?

感謝

+0

爲什麼downvoted?是合格的問題。我不會問是否可以找到方法。 – user2035693

+1

我認爲人們假設(如你所不知道的那樣),你不知道$ this'只能用在類的上下文中。也許在你的問題中顯示函數確實存在於一個類中 – Martin

回答

0

經過調試和研究,我發現,我的代碼是被包裹在一個函數的時間,所以最終的結果是像

function($var1, $var2) { return create_function()} 

這樣一個封閉的創建和$this已經不在我想要的範圍內了。

更多有關閉的位置: http://php.net/manual/ro/class.closure.php

相關問題