NYCPHP Meetup

NYPHP.org

[nycphp-talk] Why is pass-by-reference deprecated?

Rob Marscher rmarscher at beaffinitive.com
Mon Nov 19 17:07:29 EST 2007


On Nov 19, 2007, at 4:29 PM, Ben Sgro (ProjectSkyLine) wrote:
>>> Hello,
>>>
>>> Sorry to hijack, but I started thinking about something I read
>>> for optimization of php.
>>>
>>> They stated, if you know your not going to make changes
>>> to a variable, to send it to a function as reference, as to NOT
>>> make a copy of it...
>>>
>> Going back to some very, very old threads on here, I though that  
>> copies were
>> only made if the variable was changed. If the variable is not  
>> changed, I
>> believe php is smart enough to reference, not copy, the variable.
>>
>> Cliff


Yeah... you should reread this: http://www.php.net/references -- They  
updated it sometime in the last year or two.  It's much more clear  
about how references work.  Here are the highlights:

"They are not like C pointers; instead, they are symbol table aliases."

"Do not use return-by-reference to increase performance, the engine is  
smart enough to optimize this on its own. Only return references when  
you have a valid technical reason to do it!"

Also, in php5 - when you assign an object:

$a = new SomeClass();
$b = $a;

$b is actually assigned by reference.  Regular assignment of objects  
is always done by reference.  If you want to make an actual copy, you  
have to use clone():

http://us2.php.net/manual/en/language.oop5.cloning.php





More information about the talk mailing list