Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.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_Function - Fatal编程技术网

有可能生成一个可以接受任意数量参数的PHP函数吗?

有可能生成一个可以接受任意数量参数的PHP函数吗?,php,function,Php,Function,我正在编写一些PHP代码,可以从模板生成HTML文件 如果可能的话,我想创建一个函数,它可以接受我提供给函数的任何字符串,并将其放入文件中。像这样: function generator($a, $b, $c, $n...){ $filename = $a . ".html"; ob_start (); echo $b; echo $c; echo $d; echo $n...; $buffer = ob_get_clean(); file_put_contents($a, $buffer);

我正在编写一些PHP代码,可以从模板生成HTML文件

如果可能的话,我想创建一个函数,它可以接受我提供给函数的任何字符串,并将其放入文件中。像这样:

function generator($a, $b, $c, $n...){
$filename =  $a . ".html";
ob_start ();
echo $b;
echo $c;
echo $d;
echo $n...;
$buffer = ob_get_clean();
file_put_contents($a, $buffer);
}
我需要这个,因为不同的页面会有不同数量的包含文件,有了这个,我就可以跳过为特定页面创建不同的函数。只是一个迭代器,就这样


谢谢

只需使用
func_get_args()返回传入的所有参数的数组

还可以使用
func\u get\u arg($arg\u num)
返回特定参数,或使用
func\u num\u args
返回参数数

所有PHP函数都允许任意数量的参数,它们只是不能通过名称调用,唯一的方法是使用这3个函数

注意,您可以使用可变参数作为参数列表中的最后一个参数,如下所示:

function my_func($x,$y, ... $z){
    //Now $z is an array of all arguments after the first two
}

在PHP5.6+中,您可以使用
来指示可变数量的参数:

function test (... $args)
{
    foreach ($args as $arg) {
        echo $arg;
    }
}

test("testing", "variable"); // testing variable
因此,您的函数将如下所示:

function generator($a, $b, $c, ... $n) {
    $filename =  $a . ".html";
    ob_start();

    echo $b;
    echo $c;

    foreach ($n as $var) {
        echo $var;
    }

    $buffer = ob_get_clean();
    file_put_contents($a, $buffer);
}
您还可以使用:

产出:

"test"
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)

您可以使用以下数组进行设置:

function generator($array){
    // set the first item of the array as name of the .html file and take it out of the array.
  $filename = array_shift($array) . ".html";
  ob_start ();
  // echo all the array fields
  foreach($array as $a){
    echo $a;
  }
  $buffer = ob_get_clean();
  file_put_contents($a, $buffer);
}
generator( ["val_1", "val_2", "val_3"] );
您可以直接传递数组以调用函数,如下所示:

function generator($array){
    // set the first item of the array as name of the .html file and take it out of the array.
  $filename = array_shift($array) . ".html";
  ob_start ();
  // echo all the array fields
  foreach($array as $a){
    echo $a;
  }
  $buffer = ob_get_clean();
  file_put_contents($a, $buffer);
}
generator( ["val_1", "val_2", "val_3"] );

在良好的设计过程中,我会仔细考虑何时何地使用这样的东西。例如,我目前从事的一个项目可能有超过20万行的代码,而且无论是好是坏,这实际上从未被使用过

最常见的方法是将数组“struct”传递给方法:

$args = array();
$args['kitchen'] = 'sink';
$args['bath'] = 'room';
$args['cat'] = array('fur','tail');

$func->someFunction($args);
如果希望对数据有更多的控制,可以在类中创建一个结构并访问它。公共职能部门充当处理者

class SomeClass {
  ....
  private $args

  public function setArgs($arg1,$arg2,$arg3) {
      $this->arg1 = $arg1;
      ...
  }
  public function getArgs() {
      return $this->args;
  }
更难得的是,您可以使用C++类控件,在这里使用类作为结构:

class MyStruct {
    public $foo;
    public $bar;
    private $secret;
    private function getSecret() {
        return $secret;
    }
    protect function setSecret($val) {
        $secret = $val;
    }
}
已经提到的是我几乎从未见过的“…”,但它很有趣,尽管有多有用?这有助于解释发生了什么吗

  function someFunction(... $args)
通常,你会在方法中看到各种各样的东西,这有助于清楚地表达它的目的

private function someSmallFunc($list = array(), $val = '', $limit = 10)
这个例子是为了说明信息的自然分组,数据在一个列表中,$val用于控制方法以及$limit say限制查询结果的数量。因此,您应该以这种方式思考您的方法


此外,如果您注意到默认值被设置为($limit=10),以防它们没有传入。例如,如果您调用someSmallFunc($data,$someVal)(而不是说someSmallFunc($data,$someVal,20)),并且没有传入$limit,它将默认为10。

我认为您应该使用数组,这是更好的选择。非常感谢。现在看起来很简单。:-)功能方面考虑得很好。使用func_get_args的示例如何?这是一个非常详细的答案,您可能是对的。然而,对于简单的事情,我并不认为对小而简单的事情使用简单的内置函数是一件坏事。