PHP引用如何在数组引擎下工作?

Pop*_*orn 8 php arrays reference reference-counting

我正在阅读这篇关于PHP变量引用的文章:http://derickrethans.nl/talks/phparch-php-variables-article.pdf 并且想要检查我的理解是否正确,关于何时创建新的变量容器.

对于非数组,只要您指定一个未指向设置了is_ref的容器的变量,就会创建变量容器.

Ex 1 (let {..} be a container):
$a = 1;     // "a" => {integer, 1, is_ref = 0, ref_count = 1}

$b = $a;    // "a", "b" => {integer, 1, is_ref = 0, ref_count = 2}

$b = 2;     // "a" => {integer, 1, is_ref = 0, ref_count = 1}
            // "b" => {integer, 2, is_ref = 0, ref_count = 1}

Ex 2:
$a = 1;     // "a" => {integer, 1, is_ref = 0, ref_count = 1}

$b = &$a;    // "a", "b" => {integer, 1, is_ref = 1, ref_count = 2}

$b = 2;     // "a", "b" => {integer, 2, is_ref = 1, ref_count = 2}
Run Code Online (Sandbox Code Playgroud)

它如何适用于数组?它看起来不一样适用.例如,

$a = array(1, 2, 3);  
$b = $a;
$c = &$b[2];
$c = 4;
print_r($a); // prints (1, 2, 3) instead of (1, 2, 4)
print_r($b); // prints (1, 2, 4)
Run Code Online (Sandbox Code Playgroud)

我的期望:

$ a和$ b指向同一个容器.在这个容器中,我们有3个numeric_keys"0","1","2"分别指向整数1,2和3的容器.

当我们这样做时$c = &$b[2],我们更新包含整数3的容器:

  • is_ref = 0变为is_ref = 1
  • ref_count = 1变为ref_count = 2.

当我们这样做时$c = 4,我们更新包含整数3的容器:

  • 由于设置了is_ref,因此整数3变为整数4

但是,$a[2] != 4最终我的期望出了点问题.我想弄明白为什么.我最好的猜测是,当我们尝试引用数组的元素或对象的属性时,PHP引擎首先检查数组/对象本身以查看是否is_ref = 1.如果是,则一切都按照我的期望工作.如果is_ref = 0,则会发生其他事情,这就是我所看到的.有人可以填写我的"其他东西"是什么吗?

编辑 看起来这就是实际发生的事情.这段代码应该澄清一切!

$a = array(1, 2, 3);
$b = $a;
$c = &$b[2];      // $b points to a new container where $b[0], $b[1] still point to same container as $a[0], $a[1], but $b[2] points to a new container also pointed to by $c
$d = $b;        // $d points to $b's container, this means changing $c will also change $d[2]      
$d[0] = 5;      // The container pointed to by $d[0] is the same as the one pointed to by $a[0] and $b[0]. Since this container has is_ref = 0, $d[0] will now point to a new container

// At this point $a = (1, 2, 3), $b = (1, 2, 3), $c = 3, $d = (5, 2, 3)

$d[2] = 25;     // The container pointed to by $d[2] is the same as the one pointed to by $b[2] and $c. Since this container has is_ref = 1, Changing $d[2] will affect both $b[2] and $c.

// At this point $a = (1, 2, 3), $b = (1, 2, 25), $c = 25, $d = (5, 2, 25)

$e = $d[2];     // Since $d[2]'s container has is_ref = 1, $e will point to its own container

$c = 4;         // Same idea as $d[2] = 25; except $e won't get affected

// At this point $a = (1, 2, 3), $b = (1, 2, 4), $c = 4, $d = (5, 2, 4), $e = 25

// only way to have $d[2] be different from $b[2] is to make the container's is_ref = 0
unset($b[2]);
unset($c);
$b[2] = $d[2];
$d[2] = 55;

// At this point $a = (1, 2, 3), $b = (1, 2, 4), $d = (5, 2, 25), $e = 25
Run Code Online (Sandbox Code Playgroud)

Mac*_*ity 2

您创建的$a只是一个简单的变量。但是,当您创建 时$b,默认情况下,PHP 会复制该变量。So$b现在与 完全分开$a,就像在第一个示例中一样。

然后你设置$c等于对 的引用$b[2]。所以它们都指向同一个内存地址。更新一个,它就会更新另一个。问题是您认为也$a应该更新,但它不应该更新,因为它$b是它自己的变量。$b考虑一下当我们更改为对的引用时会发生什么$a

$a = array(1, 2, 3);  
$b = &$a;
$c = &$b[2];
$c = 4;
print_r($a); // prints (1, 2, 4)
print_r($b); // prints (1, 2, 4)
Run Code Online (Sandbox Code Playgroud)

这就像您所描述的那样,因为$b$a引用相同的东西(技术上$b现在是指向 的符号$a

如果您想更深入地了解该主题,这里有一篇深入介绍该主题的优秀文章。http://webandphp.com/how-php-manages-variables