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?

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

Is this the case? The docs at 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. :-)
"regular" variables are passed by value, objects are passed by reference. This is for PHP5. You can force the pass-by-reference for "regular" variables with &. In PHP4 objects ware passed by value, and you had to use & for objects.
Just seen the doc that @txyoji posted - +1
I think Jan's got it right. Here is the php5 part of the manual. php.net/manual/en/language.oop5.references.php
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.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.