dongtao9158 2016-12-14 08:12
浏览 31
已采纳

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

I have function below :

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;

}

Now $somefunction can be like below examples :

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);

every time I need to pass like 20 different functions. Is that possible I am getting error as at place of varibales I am passing function But I am not able to figure is that possible.

How I want to use :

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

Please suggest, Thanks!

  • 写回答

2条回答 默认 最新

  • dqbh8054 2016-12-14 09:23
    关注

    I see two possible options.

    Option 1

    You could use Anonymous functions. I simplified your function but you'll get the idea:

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

    Define your functions as anonymous functions:

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

    Usage:

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

    Read carefully through the documentation. Especially the part about variable scopes.

    Option 2

    Another possibility is call_user_func_array() but this requires you to make a little adjustment to cache_activity_data(). You need to add a third parameter which holds an array:

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

    Define your functions as usual:

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

    Usage

    cache_activity_data($cid, "your_function1", array("banana", "fruit"));  // shows "banana is a fruit"
    cache_activity_data($cid, "your_function2", array("banana")); // shows "banana"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器