2016-12-14 5 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

2つの可能なオプションがあります。

オプション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(" "));のように渡しているパラメータが1つもないクエリを1つだけ呼び出すと、それは問題ありません。使用法により、動作します。もう一度ありがとう:) – jas

+1

特定の関数の引数がない場合、空の配列 'array()'を渡します。 'array(" ")'は空の配列ではありません。空の文字列である1つの要素を持つ配列です。 – 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

関連する問題