PHP:在递归调用之间共享引用

发布于 2024-12-18 13:20:36 字数 1434 浏览 3 评论 0原文

我有三个函数,foobarbaz,从我的角度来看,它们应该产生相同的结果。但是,我遇到了一个问题,即递归函数调用之间共享引用。

$array = array(
    'subs' => array(
        'a' => 1,
        'b' => 2,
    ),
);

function foo(&$array, $value, $callAgain = true) {
    $subs =& $array['subs'];
    foreach ($subs as &$sub)
        $sub = $value;
    if ($callAgain) {
        $copy = $array;
        foo($copy, $value + 1, false);
    }
}

function bar(&$array, $value, $callAgain = true) {
    foreach ($array['subs'] as &$sub)
        $sub = $value;
    if ($callAgain) {
        $copy = $array;
        bar($copy, $value + 1, false);
    }
}

function baz(&$array, $value, $callAgain = true) {
    foreach ($array['subs'] as $key => $sub)
        $array['subs'][$key] = $value;
    if ($callAgain) {
        $copy = $array;
        baz($copy, $value + 1, false);
    }
}

foo($array, 3);
var_dump($array);
bar($array, 3);
var_dump($array);
baz($array, 3);
var_dump($array);

此代码产生以下结果:

array
  'subs' => 
    array
      'a' => int 4
      'b' => int 4
array
  'subs' => 
    array
      'a' => int 3
      'b' => int 4
array
  'subs' => 
    array
      'a' => int 3
      'b' => int 3

但是,我希望它们全部返回 3, 3,因为数组的副本被传递给递归调用。

如何修复前两个函数以使其返回 3, 3?我不想使用 baz 函数的语法,因为它非常冗长。

I’ve got three functions, foo, bar and baz, which, from my point of view, should produce identical results. However, I’m stuck with a problem that references are shared between recursive function calls.

$array = array(
    'subs' => array(
        'a' => 1,
        'b' => 2,
    ),
);

function foo(&$array, $value, $callAgain = true) {
    $subs =& $array['subs'];
    foreach ($subs as &$sub)
        $sub = $value;
    if ($callAgain) {
        $copy = $array;
        foo($copy, $value + 1, false);
    }
}

function bar(&$array, $value, $callAgain = true) {
    foreach ($array['subs'] as &$sub)
        $sub = $value;
    if ($callAgain) {
        $copy = $array;
        bar($copy, $value + 1, false);
    }
}

function baz(&$array, $value, $callAgain = true) {
    foreach ($array['subs'] as $key => $sub)
        $array['subs'][$key] = $value;
    if ($callAgain) {
        $copy = $array;
        baz($copy, $value + 1, false);
    }
}

foo($array, 3);
var_dump($array);
bar($array, 3);
var_dump($array);
baz($array, 3);
var_dump($array);

This code produces the following results:

array
  'subs' => 
    array
      'a' => int 4
      'b' => int 4
array
  'subs' => 
    array
      'a' => int 3
      'b' => int 4
array
  'subs' => 
    array
      'a' => int 3
      'b' => int 3

However, I expect all of them to return 3, 3, because copies of the array are passed to recursive calls.

How to fix the first two functions to make them return 3, 3? I’d prefer not to use syntax of the baz function, because it’s very verbose.

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

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

发布评论

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

评论(1

乜一 2024-12-25 13:20:36

我想你已经回答了你自己的问题—— baz 是获得你想要的行为的方法。另外两个函数的行为符合 PHP 中的预期,至少根据 参考文献的作用

但是请注意,数组内的引用存在潜在危险。对右侧的引用进行正常(非通过引用)赋值不会将左侧变成引用,但数组内的引用会保留在这些正常赋值中。这也适用于按值传递数组的函数调用。例子:

/* Assignment of array variables */
$arr = array(1);
$a =& $arr[0]; //$a and $arr[0] are in the same reference set
$arr2 = $arr; //not an assignment-by-reference!
$arr2[0]++;
/* $a == 2, $arr == array(2) */
/* The contents of $arr are changed even though it's not a reference! */
?>

换句话来说,数组的引用行为是逐个元素定义的;各个元素的引用行为与数组容器的引用状态是分离的。

I think you have answered your own question-- baz IS the way to get the behavior you want. The other two functions are behaving as intended in PHP, at least according to the manual page on What References Do:

Note, however, that references inside arrays are potentially dangerous. Doing a normal (not by reference) assignment with a reference on the right side does not turn the left side into a reference, but references inside arrays are preserved in these normal assignments. This also applies to function calls where the array is passed by value. Example:

/* Assignment of array variables */
$arr = array(1);
$a =& $arr[0]; //$a and $arr[0] are in the same reference set
$arr2 = $arr; //not an assignment-by-reference!
$arr2[0]++;
/* $a == 2, $arr == array(2) */
/* The contents of $arr are changed even though it's not a reference! */
?>

In other words, the reference behavior of arrays is defined in an element-by-element basis; the reference behavior of individual elements is dissociated from the reference status of the array container.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文