Page 1 of 1

php5 pass by reference

Posted: Thu Jan 17, 2008 4:22 pm
by thinsoldier
Update #2
ini_set('zend.ze1_compatibility_mode',0);
Just needed to turn that off. This thing has caused me tons of problems in the past with SPL classes. I think I'll just always keep it turned off from now on.

zend.ze1_compatibility_mode boolean
Enable compatibility mode with Zend Engine 1 (PHP 4). It affects the cloning, casting (objects with no properties cast to FALSE or 0), and comparing of objects. In this mode, objects are passed by value instead of reference by default.

Updated with simplified code.
Shouldn't the last line echo 'banana'?

Code: Select all

 <?
class object
{ var $testvar = ''; }
 
 
$A = new object;
$A->testvar = 'apple';
 
echo '<p>A = '.$A->testvar.'</p>';
 
$B = $A;
echo '<p>B = '.$B->testvar.'</p>';
 
$B->testvar = ' BANANA - changed testvar value with B';
 
echo '<p>A after changing B = '.$A->testvar.'</p>';
?> 
The output I get is:

Code: Select all

A = apple
 
B = apple
 
A after changing B = apple
But if I change line 11 to $B = &$A; I get what I expected:

Code: Select all

A = apple
 
B = apple
 
A after changing B = BANANA - changed testvar value with B
So my question is: Wasn't php 5 supposed to pass objects around without me needing to use the '&' all the time? It's not doing that on my server.

Re: php5 pass by reference

Posted: Thu Jan 17, 2008 5:12 pm
by pickle
Dude - ask a question.

Re: php5 pass by reference

Posted: Thu Jan 17, 2008 6:14 pm
by Ambush Commander
I think the question is in the code.

I'm under the impression that the code you've given should work as you expect it to, due to PHP passing objects "by value". Can you give us the full class definitions so I can test it out?