2016-12-14 44 views
0

我有以下功能:如何將函數參數作爲另一個本身具有不同參數的函數傳遞?

function cache_activity_data($cid,$somefunction) { 

    $cache_time = '+15 minutes'; 
    $cache_id = $cid; 
    $expire = strtotime($cache_time); 
    $cache = cache_get($cache_id); 
    if (!empty($cache->data)) { 
    if (time() > $cache->expire) { 
     cache_clear_all($cache_id, 'cache_custom_activity_dashboard'); 
     $report = $somefunction; // will get from function 
     cache_set($cache_id, $report, 'cache_custom_activity_dashboard', $expire); 
    } 
    else { 
     $report = $cache->data; 
    } 
    } 
    else { 
    $report = $somefunction; // will get from function 
    cache_set($cache_id, $report, 'cache_custom_activity_dashboard', $expire); 
    } 

    return $report; 

} 

現在$somefunction可以像下面的例子:

total_comments_per_user($user->uid); 
total_comments_per_user_time_limit($user->uid, $user_year_start); 
total_revisions_time_limit($month_ago); 
total_revisions_time_limit($year_start); 
每次我需要通過像20種不同功能的時間

。這是可能的我越來越錯誤,因爲我在傳遞函數varibales的地方但我不能夠數字是可能的。

如何我想使用:

//want to write this as function 
$cache_revisions_total = cache_get("total_revisions", "cache_custom_activity_dashboard"); 
    if (!empty($cache_revisions_total->data)) { 
    if (time() > $cache_revisions_total->expire) { 
     cache_clear_all("total_revisions", 'cache_custom_activity_dashboard'); 
     $t_revisions = total_revisions(); 
     cache_set("total_revisions", $t_revisions, 'cache_custom_activity_dashboard', $expire); 
    } 
    else { 
     $t_revisions = $cache_revisions_total->data; 
    } 
    } 
    else { 
    $t_revisions = total_revisions(); 
    cache_set("total_revisions", $t_revisions, 'cache_custom_activity_dashboard', $expire); 
    } 
// want to write this as function end here 

    $vars['total_bubbla_rev'] = number_format(($t_revisions/$days_from_rev_start), 2, '.', ''); 

// here i want to do same so i need to write function or should i repeat code 
    $y_revisions = total_revisions_time_limit($year_start); 
    $vars['yearly_bubbla_rev'] = number_format(($y_revisions/$year_days), 2, '.', ''); 

// here i want to do same so i need to write function or should i repeat code 
    $m_revisions = total_revisions_time_limit($month_ago); 
    $vars['monthly_bubbla_rev'] = number_format(($m_revisions/30), 2, '.', ''); 

請建議,謝謝!

+0

你不能傳遞你的參數的函數。但是,您可以使用回調。但在你的情況下,我不明白你爲什麼在參數中傳遞'$ somefunction'似乎毫無用處。你可以在你的問題中添加'cache_activity_data()'的調用原點嗎? –

+0

感謝您的回覆,我已經編輯了問題,因爲現在我們可以看到我需要使用函數內的數據。 – jas

+0

'$ report = $ somefunction;'我需要按照要求傳遞我的函數,這是可能的使用回調,請建議! – jas

回答

1

我看到兩種可能的選擇。

選項1

你可以使用Anonymous functions。我簡化你的功能,但你會得到的想法:

function cache_activity_data($cid, $somefunction) { 
    $report = $somefunction(); 
} 

定義函數,匿名函數:

$parm1 = "banana"; 
$parm2 = "fruit"; 

$your_function1 = function() use ($parm1, $parm2) { 
    echo "$parm1 is a $parm2"; 
}; 

$your_function2 = function() use ($parm1) { 
    echo $parm1; 
}; 

用法:

cache_activity_data($cid, $your_function1); // shows "banana is a fruit" 
cache_activity_data($cid, $your_function2); // shows "banana" 

通過文件仔細閱讀。特別是關於可變範圍的部分。

選項2

另一種可能性是call_user_func_array(),但是這需要你做出一點調整cache_activity_data()。您需要添加持有數組中的第三個參數:

function cache_activity_data($cid, $somefunction, $somefunction_parms) { 
    $report = call_user_func_array($somefunction, $somefunction_parms); 
} 

定義函數,像往常一樣:

function your_function1($parm1, $parm2) { 
    echo "$parm1 is a $parm2"; 
} 

function your_function2($parm) { 
    echo $parm; 
} 

使用

cache_activity_data($cid, "your_function1", array("banana", "fruit")); // shows "banana is a fruit" 
cache_activity_data($cid, "your_function2", array("banana")); // shows "banana" 
+0

非常感謝你爲這個真棒解釋和方式。只是一個查詢,其中我的函數沒有任何參數,我正在傳遞像'cache_activity_data($ cid,「your_function3」,array(「」));'這樣會好的。通過使用它的作品。再次感謝:) – jas

+1

如果你沒有某個函數的參數傳遞一個空數組:'array()'。 'array(「」)'不是一個空數組,它是一個數組,它是一個空字符串。 – simon

+0

再次感謝您的幫助! – jas

1

首先,你不能傳遞函數作爲參數,但是你可以使用回調如下解釋: http://php.net/manual/en/language.types.callable.php

但在你的情況,這似乎無關緊要你是不是確定的功能或cache_activity_data()改變其值。

因此,你可能想要做這樣的:

$reportDefault = total_comments_per_user($user->uid); 
// Or ... $reportDefault = total_revisions_time_limit, total_comments_per_user_time_limit, etc.. 
$report = cache_activity_data($cid, $reportDefault); 

你並不需要添加通$report或參數的任何功能。

+0

但是,在$報告的位置,我需要使用不同函數獲取的不同數據,所以我猜這不可能使用單個函數來緩存來自不同函數的數據? – jas

+0

感謝您的幫助和努力+1對我來說可能並不清楚,或者我無法清除我現在會嘗試更多。這樣就不會有緩存的用戶如果我在調用緩存函數之前調用函數。 – jas

相關問題