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

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False