Mauricio Zuniga
Mauricio Zuniga

Reputation: 43

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

Upvotes: 3

Views: 667

Answers (3)

sectus
sectus

Reputation: 15464

Also you could use json_decode($json, true); (instead of json_decode($json);) to get assoc array (instead of stdClass).

And there would not any issues with references.

Upvotes: 2

Mauricio Zuniga
Mauricio Zuniga

Reputation: 43

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

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

and this actually did the trick!

Upvotes: 0

sectus
sectus

Reputation: 15464

Since PHP5 object always pass by reference. If you want to get copy of object you have to use 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.

Assignment Operators

Upvotes: 6

Related Questions