私は、以下の機能があります。今どうやって関数パラメータを別の関数として渡すことができますか?
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, '.', '');
、感謝を提案してください!
あなたのパラメータで関数を渡すことはできませんあなたは、配列を保持している三番目のパラメータを追加する必要があります。ただし、コールバックを使用することはできます。しかし、あなたのケースでは、なぜあなたが '$ somefunction'をパラメータとして渡しているのか分かりませんが、それは役に立たないようです。'cache_activity_data()'の呼び出し元を質問に追加できますか? –
返信いただきありがとうございます。今私は私の関数を使ってデータを関数内に必要とすることがわかりました。 – jas
'$ report = $ somefunction;'どこに私の関数を渡す必要があるのですか?それはコールバックを使って可能でしょうか、提案してください! – jas