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

相关问题