函数中的可变长度 by-ref 参数列表

发布于 2024-12-08 11:08:57 字数 435 浏览 0 评论 0原文

在 PHP 中,你可以这样做:

function something() {
  foreach (func_get_args() as $arg)
    echo $arg;
}
something(1, 3); //echoes "13"

这对于按值传递的参数来说效果很好,但如果我希望它们按引用传递怎么办?像这样:

function something_else() {
  foreach (func_get_args() as $arg)
    $arg *= 2;
}
$a = 1;
$b = 3;
something_else($a, $b);
echo $a . $b; //should echo "26", but returns "13" when I try it

这在 PHP 中可能吗?

In PHP you can do this:

function something() {
  foreach (func_get_args() as $arg)
    echo $arg;
}
something(1, 3); //echoes "13"

This works fine for arguments passed by value, but what if I want them to be passed by reference? like this:

function something_else() {
  foreach (func_get_args() as $arg)
    $arg *= 2;
}
$a = 1;
$b = 3;
something_else($a, $b);
echo $a . $b; //should echo "26", but returns "13" when I try it

Is this possible in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尹雨沫 2024-12-15 11:08:57

这个问题看起来很可怕,但让你幽默一下。下面是一个可怕的黑客,但您可以发送一个包含您想要使用的项目的参数。

function something_else($args) {
    foreach ($args as &$arg) {
        $arg *= 2;
    }
}
$a = 1;
$b = 3;
something_else(array(&$a, &$b));
echo $a . $b; // 26

The question seems horrible, but lets humour you. Below is a horrible hack, but you could send across a single argument which contains the items that you want to work with.

function something_else($args) {
    foreach ($args as &$arg) {
        $arg *= 2;
    }
}
$a = 1;
$b = 3;
something_else(array(&$a, &$b));
echo $a . $b; // 26
潦草背影 2024-12-15 11:08:57

不,你不能。由 ref 传递的参数的声明显式为 function Something(&$arg1, &$arg2)。如果您在编译时不知道参数的数量,可以执行以下操作:

function something_else($args) {
      foreach ($args as $arg)
        $GLOBALS[$arg] *= 2;
}
$a = 1;
$b = 3;
something_else(array('a', 'b'));
echo $a . $b; //returns "26"

基本上,代码将函数将修改的参数名称传递给函数。 $GLOBALS 保存对脚本全局范围内所有已定义变量的引用。这意味着如果调用来自另一个函数,它将不起作用:

function something_else($args) {
      foreach ($args as $arg)
        $GLOBALS[$arg] *= 2;
}

function other_function(){
    $a = 1;
    $b = 3;
    something_else(array('a', 'b'));
    echo $a . $b; //returns "13"
}
other_function();

触发器会注意到未定义的索引 ab。因此,另一种方法是创建一个数组,其中引用函数将修改的变量,如下所示:

function something_else($args) {
      foreach ($args as &$arg)
        $arg *= 2;
}

function other_fucntion(){
    $a = 1;
    $b = 3;
    something_else(array(&$a, &$b));
    echo $a . $b; //returns "26"
}
other_fucntion();

注意 foreach 行上的 & 。这是需要的,这样就不会创建一个迭代数组的新变量。 PHP>此功能需要 5 个。

No. You cannot. The declaration of a prameter passed by ref is explicit as function something(&$arg1, &$arg2). If you don't know the number of parameters at compile time, you can do something like this:

function something_else($args) {
      foreach ($args as $arg)
        $GLOBALS[$arg] *= 2;
}
$a = 1;
$b = 3;
something_else(array('a', 'b'));
echo $a . $b; //returns "26"

Basically the code passes to the function the names of the params the function will modify. $GLOBALS holds references to all defined variables in the global scope of the script. This means if the call is from another function it will not work:

function something_else($args) {
      foreach ($args as $arg)
        $GLOBALS[$arg] *= 2;
}

function other_function(){
    $a = 1;
    $b = 3;
    something_else(array('a', 'b'));
    echo $a . $b; //returns "13"
}
other_function();

triggers notices undefined indexes a and b. So another approach is to create an array with references to the variables the function will modify as:

function something_else($args) {
      foreach ($args as &$arg)
        $arg *= 2;
}

function other_fucntion(){
    $a = 1;
    $b = 3;
    something_else(array(&$a, &$b));
    echo $a . $b; //returns "26"
}
other_fucntion();

Note the & on the foreach line. It is needed so not to create a new variable iterating over the array. PHP > 5 needed for this feature.

痴梦一场 2024-12-15 11:08:57

您可以这样做,但它使用调用时传递引用,这在 PHP 5.3 中已弃用:

function something_else() {
    $backtrace = debug_backtrace();
    foreach($backtrace[0]['args'] as &$arg)
    $arg *= 2;
}
$a = 1;
$b = 3;
something_else(&$a, &$b);
echo $a . $b;

You can do it this way but it uses call-time pass by reference which is deprecated in PHP 5.3:

function something_else() {
    $backtrace = debug_backtrace();
    foreach($backtrace[0]['args'] as &$arg)
    $arg *= 2;
}
$a = 1;
$b = 3;
something_else(&$a, &$b);
echo $a . $b;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文