php5 pass by reference
Posted: Thu Jan 17, 2008 4:22 pm
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'?
The output I get is:
But if I change line 11 to $B = &$A; I get what I expected:
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.
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>';
?> Code: Select all
A = apple
B = apple
A after changing B = appleCode: Select all
A = apple
B = apple
A after changing B = BANANA - changed testvar value with B