2017-01-24 62 views
0

我:

$this->addRow('$this->userRepo->dateTargetedActual', [$teamMember, $date->year, $date->month], $year, 'Stage target', 'target', $teamMember->image); 

但它給我的錯誤:

call_user_func_array() expects parameter 1 to be a valid callback, function '$this->userRepo->dateTargetedActual' not found or invalid function name 

我如何得到這個工作?


protected function addRow($func, $params, $data, $year, $traderName, $rowName, $type, $image = null) { 
    $date = Carbon::createFromDate($year, 4, 1); 
    $total = 0; 
    $traderName = $traderName.' '.str_replace(' ', '_', strtolower($rowName)); 
    $params[] = $date->month; 
    for ($i = 1; $i < 13; $i++) { 
     $result = call_user_func_array($func, $params); 
     $data['traders'][$traderName]['amounts'][] = $result; 
     $total += $result; 
     $date->addMonth(); 
    } 
    $data['traders'][$traderName]['amounts'][] = $total; 
    $data['traders'][$traderName]['image'] = $image; 
    $data['traders'][$traderName]['row_name'] = $rowName; 
    $data['traders'][$traderName]['row_type'] = $type; 
    return $data; 
} 
+0

$this->addRow($this->userRepo->dateTargetedActual, [$teamMember, $date->year, $date->month], $year, 'Stage target', 'target', $teamMember->image); 

如果$this->userRepo->dateTargetedActual擁有一個數據,你需要一個回調函數返回的數據塊,那麼你就可以定義內嵌的函數調用裏面''?另外如果dateTargetedActualy是你想用的函數(),所以我認爲這應該是 $ this-> addRow($ this-> userRepo-> dateTargetedActual(),[$ teamMember,$ date-> year,$ $ year,'Stage target','target',$ teamMember-> image); –

+0

'$ this-> userRepo-> dateTargetedActual'的值是什麼?它存儲了一個函數嗎?或者你想讓'addRow()'函數在'$ this-> userRepo'對象上調用'dateTargetedActual()'嗎? – patricus

回答

0

我想你不應該換$this->userRepo->dateTargetedActual爲字符串。

試試這個: $this->addRow($this->userRepo->dateTargetedActual(), [$teamMember, $date->year, $date->month], $year, 'Stage target', 'target', $teamMember->image);

+0

[$ this-> userRepo,'dateStagesTarget']呢? – imperium2335

0

由於我們沒有爲addRow的代碼,我們基於這樣到addRow第一個參數被作爲第一個參數傳遞給call_user_func_array()的錯誤信息,猜測哪些期望第一個參數是回調。

如果$this->userRepo->dateTargetedActual實際上持有回調函數,那麼您只需要在不引用它的情況下傳遞該值,它將由call_user_func_array()函數執行。爲什麼你傳遞一個變量

$this->addRow(
    function() { 
     return $this->userRepo->dateTargetedActual; 
    }, 
    [$teamMember, $date->year, $date->month], 
    $year, 
    'Stage target', 
    'target', 
    $teamMember->image 
); 
+0

爲addRow函數添加代碼。 – imperium2335