gpt4 book ai didi

php - 关于 ZVal 的说明

转载 作者:可可西里 更新时间:2023-10-31 22:13:12 25 4
gpt4 key购买 nike

我正在读这个:http://www.dereleased.com/2011/04/27/the-importance-of-zvals-and-circular-references/

还有一个例子让我有点迷失了。

$foo = &$bar;
$bar = &$foo;
$baz = 'baz';

$foo = &$baz;

var_dump($foo, $bar);
/*
string(3) "baz"
NULL
*/

If you’ve been following along, this should make perfect sense. $foo is created, and pointed at a ZVal location identified by $bar; when $bar is created, it points at the same place $foo was pointed. That location, of course, is null. When $foo is reassigned, the only thing that changes is to which ZVal $foo points; if we had assigned a different value to $foo first, then $bar would still retain that value.

我学会了用 C 编程。我知道 PHP 是不同的,它使用 ZVals 而不是内存位置作为引用。但是当你运行这段代码时:

$foo = &$bar;
$bar = &$foo;

在我看来,应该有两个 ZVal。在 C 中会有两个内存位置(并且值将位于相反的内存位置)。

谁能解释一下?

最佳答案

这主要取决于符号表 的工作方式。这是一张有两个面的 table :

symbol name  |  value
-------------+-------
|

有趣的是,可以为一个值分配多个名称:

symbol name  |  value
-------------+-------
foo, bar | 'baz'

当使用 = 分配给一个交易品种时,表格的 value 端会发生变化:

$baz = 42;

symbol name | value
-------------+-------
baz | 42

当使用 =& 赋值时,symbol name 边被移动到值所在的位置:

$foo =& $baz;

symbol name | value
-------------+-------
baz, foo | 42

因此在您的示例中,从头开始:

$foo =& $bar;
($bar does not exist, is null, which is implicitly created,
$foo is pointed to where the implicitly created $bar points)

symbol name | value
-------------+-------
foo, bar | null


$bar = &$foo;
(no real change, $bar is pointed to where $foo is pointing)

symbol name | value
-------------+-------
foo, bar | null


$baz = 'baz';

symbol name | value
-------------+-------
foo, bar | null
baz | 'baz'


$foo = &$baz;
($foo is pointed to where $baz is pointing)

symbol name | value
-------------+-------
bar | null
baz, foo | 'baz'

关于php - 关于 ZVal 的说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13937588/

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