PHP引用如何在数组引擎下工作?
我正在阅读有关
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,ref_count = 2} $b = 2; // "a" => {integer,ref_count = 1} // "b" => {integer,2,ref_count = 1} Ex 2: $a = 1; // "a" => {integer,ref_count = 1} $b = &$a; // "a",is_ref = 1,ref_count = 2} $b = 2; // "a",ref_count = 2} 它如何适用于数组?它看起来不一样适用.例如, $a = array(1,3); $b = $a; $c = &$b[2]; $c = 4; print_r($a); // prints (1,3) instead of (1,4) print_r($b); // prints (1,4) 我的期望: $a和$b指向同一个容器.在这个容器中,我们有3个numeric_keys“0”,“1”,“2”分别指向整数1,2和3的容器. 当我们执行$c =& $b [2]时,我们更新包含整数3的容器: > is_ref = 0变为is_ref = 1 当我们执行$c = 4时,我们更新包含整数3的容器: >因为设置了is_ref,整数3变为整数4 但是,我的期望出了点问题,因为$a [2]!= 4最后.我想弄明白为什么.我最好的猜测是,当我们尝试引用数组的元素或对象的属性时,PHP引擎首先检查数组/对象本身以查看是否is_ref = 1.如果是,则一切都按照我的期望工作.如果is_ref = 0,则会发生其他事情,这就是我所看到的.有人可以填写我的“其他东西”是什么吗? 编辑 $a = array(1,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,3),$b = (1,$c = 3,$d = (5,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,25),$c = 25,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,4),$c = 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,$e = 25
你创造了什么$a它是一个简单的变量.但是当你创建$b时,默认情况下,PHP复制了变量.所以$b现在与$a完全分开,就像你在第一个例子中那样.
然后将$c设置为等于$b [2]的引用.所以他们都指向相同的内存地址.更新一个,它更新另一个.问题是你认为$a也应该更新,但不应该因为$b是它自己的变量.考虑当我们将$b更改为$a的引用时会发生什么 $a = array(1,3); $b = &$a; $c = &$b[2]; $c = 4; print_r($a); // prints (1,4) 这就像你描述的那样,因为$b和$a引用相同的东西(技术上$b现在是一个指向$a的符号) 如果你想更深入地探讨这个主题,这里有一篇很好的文章,深入介绍它. http://webandphp.com/how-php-manages-variables (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- php中flush()、ob_flush()、ob_end_flush()的区别
- PHP Image / Fileupload:crossbrowser,多个文件
- ECshop 迁移到 PHP7版本时遇到的兼容性问题
- php – file_get_contents与相对路径
- 如何自学Struts2之Struts2结果类型[视频]
- php – $this-> render()和$this-> redirect()之
- php – 如何在不使用foreach构造的情况下组合数组
- Yii2隐藏frontend/web和backend/web的方法
- PHP的__METHOD__魔术常量用法
- PHP如何通过AJAX方式实现登录功能