简体   繁体   English

PHP Pass By Reference问题

[英]PHP Pass by Reference Issue

So I am having a strange issue where the functions are NOT defined by pass by reference parameters, yet objects are being changed in ways that I cannot explain. 所以我遇到一个奇怪的问题,即函数没有通过引用参数传递来定义,但是对象正在以我无法解释的方式进行更改。 I have verified function definitions are not pass by reference time and time again. 我已经验证了函数定义没有通过引用一次又一次地传递。 I have retrieved an object from the DB. 我从DB中检索了一个对象。 Then I have run an analysis function on that initial object. 然后我在该初始对象上运行了分析函数。 I have copied the object to another variable. 我已将对象复制到另一个变量。 Then I run a different analysis function on the copy and not the original. 然后我在副本上运行不同的分析功能而不是原始分析功能。 Running the second analysis function seems to alter the first variable-object. 运行第二个分析函数似乎改变了第一个变量对象。 Any ideas on what might be going on here. 关于可能会发生什么的任何想法。 I have been trying to debug this for hours upon hours and I cannot explain this behavior. 我一直试图调试这几个小时,我无法解释这种行为。 I would prefer not to post the the actual functions as they are proprietary information, however, I can possibly send them privately for some help. 我不希望发布实际功能,因为它们是专有信息,但是,我可以私下发送它们以获得一些帮助。 I thank you kindly for your time in trying to help me. 我很感谢你的时间来帮助我。

//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. )
*/

Since PHP5 object always pass by reference. 由于PHP5对象总是通过引用传递。 If you want to get copy of object you have to use clone . 如果要获取对象的副本,则必须使用clone

Objects and references 对象和参考

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中的值行为通常赋值的例外情况发生在对象上,这些对象在PHP 5中通过引用分配。对象可以通过clone关键字显式复制。

Assignment Operators 分配运营商

Also you could use json_decode($json, true); 你也可以使用json_decode($json, true); (instead of json_decode($json); ) to get assoc array (instead of stdClass ). (而不是json_decode($json); )来获取assoc array (而不是stdClass )。

And there would not any issues with references. 并且引用没有任何问题。

after going crazy forever... I found this solution: 永远疯了之后...我找到了这个解决方案:

$r2 = unserialize(serialize($resp));

I know it is not ideal as there is a performance hit, but I am under a deadline and needed to create a working solution as soon as possible. 我知道它并不理想,因为性能受到了打击,但我处于最后期限之前,需要尽快创建一个可行的解决方案。 I believe that issue remained because even the variables that are being copied are also passed by reference. 我认为这个问题仍然存在,因为即使是被复制的变量也通过引用传递。 I am willing to accept another working solution if someone comes up with a better alternative. 如果有人提出更好的选择,我愿意接受另一种有效的解决方案。 Thank you! 谢谢!

also... due to some other issues with serialization (libxml cannot be serialized), this solution didn't work... but then I thought of 还...由于序列化的其他一些问题(libxml无法序列化),这个解决方案没有用......但后来我想到了

$r2 = json_decode(json_encode($resp));

and this actually did the trick! 这实际上就是诀窍!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM