gpt4 book ai didi

php - 有人可以解释这 2 个引用用法在 PHP 中的区别吗?

转载 作者:可可西里 更新时间:2023-11-01 13:32:11 26 4
gpt4 key购买 nike

下面的代码说明了 PHP 引用的奇怪行为:

<?php

function this_works()
{
$root = array('name'=>'root', 'children'=>array());
$level_1 = array('name'=>'level_1', 'children'=>array());
$item1 = array('name'=>'level_2_1', 'children'=>array());
$item2 = array('name'=>'level_2_2', 'children'=>array());

$croot = &$root;

$croot['children'][] = &$level_1;

$croot = &$level_1;

$croot['children'][] = &$item1;
$croot['children'][] = &$item2;

$croot = &$root;

print_r($croot);
}

function this_fails()
{
$root = array('name'=>'root', 'children'=>array());
$level_1 = array('name'=>'level_1', 'children'=>array());
$item1 = array('name'=>'level_2_1', 'children'=>array());
$item2 = array('name'=>'level_2_2', 'children'=>array());
$croot = &$root;

$stack = array();

$croot['children'][] = &$level_1;
$crootref = &$croot;

array_push($stack, $crootref);

$croot = &$level_1;

$croot['children'][] = &$item1;
$croot['children'][] = &$item2;

# this works, assignment below - doesn't... WHY?
#$x = array_pop($stack);
#var_dump($x);

$croot = array_pop($stack);

print_r($croot);
}

this_works();
echo "------------------\n";
this_fails();

?>

第一个函数提供了预期的结果,而第二个失败并声称递归循环:

Array
(
[name] => root
[children] => Array
(
[0] => Array
(
[name] => level_1
[children] => Array
(
[0] => Array
(
[name] => level_2_1
[children] => Array
(
)

)

[1] => Array
(
[name] => level_2_2
[children] => Array
(
)

)

)

)

)

)
------------------
Array
(
[name] => root
[children] => Array
(
[0] => Array
(
[name] => root
[children] => Array
*RECURSION*
)

)

)

奇怪的是,如果在第二个函数中,中间变量将用于从堆栈中获取值,结果再次正常。我不明白这是怎么回事。我如何获得根元素作为由于一次攻击,多次成为自己的 child ?

最初,我需要从 XML 构建树(使用 sax 解析器)并打算让“当前根”指向树节点当前级别并将其插入/弹出堆栈并向其添加子元素,但是,令人惊讶的是,由于所展示的问题,我未能实现该方案通过以上两个函数。

那么,这种做法有什么问题呢?

最佳答案

这是因为 PHP 在数组内部引用的方式很奇怪。在右侧使用引用进行正常(不是通过引用)赋值通常不会将左侧变成引用,但数组内部的引用会保留在赋值中,即使没有引用运算符也是如此。

我在下面评论了您的代码以帮助解释正在发生的事情,我希望我已经正确解释了这一点,现在是凌晨 1 点,我有点累了。

$root = array('name'=>'root', 'children'=>array());
$level_1 = array('name'=>'level_1', 'children'=>array());
// $croot and $root now point to the same variable
$croot = &$root;

$stack = array();

$croot['children'][] = &$level_1;
// $crootref, $croot and $root all now point to the same variable
$crootref = &$croot;
// $stack[0], $crootref, $croot and $root all now point to the same variable.
// $stack[0]['children'][0], $level_1, $croot['children'][0] point to the same variable
array_push($stack, $crootref);
// $croot, $level_1 and $stack[0]['children'][0] now point to the same variable
// Infinite loop is caused as $stack[0]['children'][0] is now an alias for $croot
// which contains $croot['children'][0] which is an alias for $stack[0]['children'][0]
// which is an alias for $croot which contains....
$croot = &$level_1;

关于php - 有人可以解释这 2 个引用用法在 PHP 中的区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12736552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com