dongxiejie9387 2014-09-08 01:36 采纳率: 100%
浏览 88

PHP - 将参数作为数组传递给函数,而不必定义为参数

Is there away to pass a variable (with array data) into a function without having to use as a parameter each time I want to use the function?

My situation is that I have producing UI elements for a form. Right now if I define the input's name I get the field which works as designed, but I also have to pass two variables every time.

Function Defined:

function getDecision($name,$game,$answer) { 
  // lots of code not very relevant 
}

Ideally I want to pass the $game and $answer variables inside the function for what is dependent on them. I am using code igniter and the $game variable is passed to the view on load of the page. The $answer variable can be defined within function only if the $game has been passed or define.

Current use of the function:

getDecision('company_name', $game, $answer);

Ideal use of function (simpler use):

getDecision('company_name');

Let me know if there is anything else I need to define, I don't want to show all of the code because there is nearly 100 lines of code.

  • 写回答

2条回答 默认 最新

  • dscqrkvr9562034621 2014-09-08 02:30
    关注

    Using closures for this would be a good option - http://php.net/manual/en/functions.anonymous.php, specifically the 'use' construct

    #!/usr/bin/env php 
    <?php
    
    function getDecision($name, $game, $answer) {
      echo "$name : $game : $answer 
    ";
    }
    
    function main() {
    
      $my_game = "my game";
      $my_answer = "my answer";
    
    
      $getDecisionCaller = function($name) use ($my_game, $my_answer) {
        getDecision($name, $my_game, $my_answer);
      };  
    
      // Don't really need the line below, can simply use: $getDecisionCaller('company 0');
      getDecision('company 0', $my_game, $my_answer);
    
      $getDecisionCaller('company 1');
      $getDecisionCaller('company 2');
    }
    
    main();
    
    ?>
    

    Will give the output

    company 0 : my game : my answer 
    company 1 : my game : my answer 
    company 2 : my game : my answer
    

    In the code above i define getDecision($name, $game, $answer) that simply prints out the values sent. Then, i define getDecisionCaller that accepts only $name an an argument but uses the values of $my_game, $my_answer already defined.

    Note - The 'use' construct needs variables to bind to, and you cannot set the values via a string. It will give the error - 'Parse error: parse error, expecting '&'' or"variable (T_VARIABLE)"''

    eg. You cannot do the below

    $getDecisionCaller = function($name) use ('a game', 'an answer') {
      getDecision($name, $my_game, $my_answer);
    };
    

    Hopefully, that helped :)

    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染