tags:

views:

53

answers:

2

I know there's the option to pass a value by reference, simply by writing something like

function foo(&$bar) {...}

But from Objective-C I'm used to believe that objects are always passed by reference (since the variables referencing them are just pointers whose values are just a memory address). Only sctructures and primitives would have to explicitely be passed by reference.

How's that going on in PHP? Must I be sure to put that address operator & into every parameter to enhance performance when passing objects?

+3  A: 

Only in versions prior to PHP5. In PHP5 objects are always passed by reference.

Jan Hančič
Is this the case? The docs at http://www.php.net/manual/en/language.references.pass.php would seem to suggest otherwise. Then again, I'm relying on the PHP docs being accurate and up to date which isn't always the best idea. :-)
middaparka
Jan Hančič
Just seen the doc that @txyoji posted - +1
middaparka
I think Jan's got it right. Here is the php5 part of the manual. http://www.php.net/manual/en/language.oop5.references.php
txyoji
And though the doc specifically says `"objects are passed by references by default". This is not completely true` the object itself is not passed by value. Only the "object identifier" is passed by value but it "references" the same object.
VolkerK
A: 

PHP5 passes object by reference. To pass them by value you can use the keyword clone.

dblackshell