我们如何将函数参数作为另一个本身具有不同参数的函数传递?

时间:2016-12-14 08:12:58

标签: php function parameters

我的功能如下:

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, '.', '');

请建议,谢谢!

2 个答案:

答案 0 :(得分: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);

您无需添加pass $report或任何函数作为参数。

答案 1 :(得分: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"