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

时间:2014-10-02 19:16:50

标签: 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}

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

$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)

我的期望:

$ 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的容器:

  • 整数3变为整数4,因为is_ref已设置

然而,我的期望出了点问题,因为最后$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

1 个答案:

答案 0 :(得分:2)

您创建的内容$a这是一个简单的变量。但是当你创建$b时,默认情况下,PHP复制了变量。所以$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)

这与您描述的一样,因为$b$a引用相同的内容(技术上$b现在是指向$a的符号)

如果你想深入研究这个主题,这里有一篇很好的文章,深入介绍。 http://webandphp.com/how-php-manages-variables