PHP Pass By Reference问题
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP Pass By Reference问题,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1627字,纯文字阅读大概需要3分钟。
内容图文
![PHP Pass By Reference问题](/upload/InfoBanner/zyjiaocheng/749/6702987a97664f309f7238d5948f4831.jpg)
所以我遇到一个奇怪的问题,即函数没有通过引用参数传递来定义,但是对象正在以我无法解释的方式进行更改.我已经验证了函数定义没有通过引用一次又一次地传递.我从DB中检索了一个对象.然后我在该初始对象上运行了分析函数.我已将对象复制到另一个变量.然后我在副本上运行不同的分析功能而不是原始分析功能.运行第二个分析函数似乎改变了第一个变量对象.关于可能会发生什么的任何想法.我一直试图调试这几个小时,我无法解释这种行为.我不希望发布实际功能,因为它们是专有信息,但是,我可以私下发送它们以获得一些帮助.我很感谢你的时间来帮助我.
//get object from db
$resp= json_decode($ln->getResponseFromDb($resultid));
//run pwf analysis function
$resp = $ln->pwfBGCheck($resp);
//show result after pwf
print_r($resp->pwf);
/* shows
* stdClass Object ( [status] => p [reason] => Person has no c record. )
*/
//copy to another variable
$r2 = $resp;
//run pwf for s record other variable so it is not touching the first one!
$r2 = $ln->pwfBGCheckSexOffender2($r2);
echo '<BR>this is first variable<BR>';
print_r($resp->pwf);
/* copies from second to first for some reason... no pass by reference on this call... resp variable has not been touched!
* stdClass Object ( [status] => p [reason] => Person has no s record. )
*/
echo '<BR>this is second<BR>';
print_r($r2->pwf);
/* returns
* stdClass Object ( [status] => p [reason] => Person has no s record. )
*/
解决方法:
由于PHP5对象总是通过引用传递.如果你想获得对象的副本,你必须使用clone.
An exception to the usual assignment by value behaviour within PHP occurs with objects, which are assigned by reference in PHP 5. Objects may be explicitly copied via the clone keyword.
内容总结
以上是互联网集市为您收集整理的PHP Pass By Reference问题全部内容,希望文章能够帮你解决PHP Pass By Reference问题所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。