Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 动态函数、可变输入_Php_Dynamic Variables_Dynamic Function - Fatal编程技术网

Php 动态函数、可变输入

Php 动态函数、可变输入,php,dynamic-variables,dynamic-function,Php,Dynamic Variables,Dynamic Function,现在,假设我有这样的代码 $some_var=returnsUserInput(); function funcA($a) {...} function funcB($a,$b) {...} function funcC($a,$b,$c) {...} $list[functionA] = "funcA"; $list[functionB] = "funcB"; $list[functionC] = "funcC"; $temp_call = list[$some_var]; //Not

现在,假设我有这样的代码

$some_var=returnsUserInput();

function funcA($a) {...}
function funcB($a,$b) {...}
function funcC($a,$b,$c) {...}

$list[functionA] = "funcA";
$list[functionB] = "funcB";
$list[functionC] = "funcC";

$temp_call = list[$some_var];

//Not sure how to do this below, just an example to show the idea of what I want.
$temp_call($varC1,varC2,$varC3);
$temp_call($varB1,varB2);
$temp_call($varA1);
我的问题从这里开始,如何根据这些参数在参数中指定适当的变量?我有一些想法,比如为指定这些的每个函数创建一个列表,但我真的希望看到一个优雅的解决方案。

您需要使用或调用\u user\u func\u数组

<?php
// if you know the parameters in advance.
call_user_func($temp_call, $varC1, $varC2);
// If you have an array of params.
call_user_func_array($temp_call, array($varB1, $varB2));
?>

你不能这样做,也许这样做很好。您可以使用if/else找到参数的数量


如果($temp_call==“funcA”){….}elseif(…)…

您想要以下内容吗

function test()
{
    $num_args   =   func_num_args();

    $args       =   func_get_args();

    switch ($num_args) {
        case 0:
            return 'none';
        break;


        case 1: 
            return $args[0];

        break;

        case 2:
            return $args[0] . ' - ' . $args[1];
        break;

        default:

            return implode($args, ' - ');
        break;
    }
}

echo test(); // 'none'
echo test(1); // 1
echo test(1, 2); // 1 - 2
echo test(1, 2, 3); // 1 - 2 - 3
它将充当某种委托方法

或者只接受数组而不是参数呢

function funcA($params) 
{
  extract($params);

  echo $a;
}

function funcB($params) 
{
  extract($params);

  echo $a, $b;
}

function funcC($params) 
{
  extract($params);

  echo $a, $b, $c;
}


$funcs = array('funcA', 'funcB', 'funcC');

$selected = $funcs[0];


$selected(array('a' => 'test', 'b' => 'test2'));

// or something like  (beware of security issues)
$selected($_GET);

您希望
func()
如何处理一个单参数调用?我想问的是@Igancio的可能重复。我有基于其他逻辑选择每个函数的逻辑,但我希望能够分配函数,并使用正确的参数调用它。@John不,我不是在问这个问题。我有一组来自请求的特定数据,我希望将其传递给特定函数(已在数组中定义),但我希望能够创建一个可变函数,该函数也可以动态分配并提供特定参数。我不理解这个问题<代码>$temp\u call($var,$var,$var)在PHP中是绝对正确的。这不是计算参数的问题。它们不是var1、var2和var3。它们是varA1、varA2、varA3作为一个系列,varB1、varB2作为一个系列,varC1作为它自己的系列。我可以把VarQ1、varQ2和super_duper作为一个系列。不,它们是3个函数,而不是1个。它们都做着非常不同的事情。我曾想过接受一个数组,但我必须改变大约一半的代码基础,使其能够工作,然后我处理可能有可变函数的问题,等等。这看起来是朝着正确方向迈出的一步。在我玩了一会儿之后,我会让你知道的。谢谢。另外,请不要使用
call\u user\u func
stuff,除非您正在动态调用一个方法,因为它有两个问题:它可能无法通过引用传递,而且速度很慢;)谢谢,我打算使用call_user_func_数组,仅供参考。