4

Are Php function parameters passed as references to object or as copy of object?

It's very clear in C++ but in Php5 I don't know.

Example:

<?php $dom = new MyDocumentHTMLDom(); myFun($dom); 

Is parameter $dom passed as a reference or as a copy?

6 Answers 6

9

In PHP5, objects are passed by reference. Well, not exactly in technical way, because it's a copy - but object variables in PHP5 are storing object IDENTIFIER, not the object itself, so essentially it's the same as passing by reference.

More here: http://www.php.net/manual/en/language.oop5.references.php

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

2 Comments

"so essentially it's the same as passing by reference." No it is not at all. Pass by reference means you can modify the variable in the passing scope; since the variable is the object identifier, this means you can modify the object identifier in the calling scope from the function. This is not possible unless you explicitly pass by value (&), same with all other types.
In the scope of this question, it is the same. The question was about passing object clone/copy or the same object to function call. In PHP "object" variables are essentially object identifiers which are handled at low level by PHP engine. From the developer point of view there's no such id present, but the object itself. And afformentioned object behaves as it was passed by reference. So we see the "object" variable which we can modify freely as it was passed by reference. What happens at low level is not important here, as developer doesn't have easy access to it anyway.
1

Objects are always pass by reference, so any modifications made to the object in your function are reflected in the original

Scalars are pass by reference. However, if you modify the scalar variable in your code, then PHP will take a local copy and modify that... unless you explicitly used the & to indicate pass by reference in the function definition, in which case modification is to the original

2 Comments

"Objects are always pass by reference" NO, objects are not values, and cannot be "passed". You have an object reference (or object identifier), which can be passed by value or by reference, same with all other types of values.
@newacct - nice to know that pedants will still downvote 2-year old answers: do you really believe that you're adding value to StackOverflow, or is it simply how you get your jollies?
1

In PHP5, the default for objects is to pass by reference.

Here is one blog post that highlights this: http://mjtsai.com/blog/2004/07/15/php-5-object-references/

1 Comment

That's incorrect. It passes a copy of an object identifier. This creates a new zval struct in the internal symbol table. Passing by reference in PHP uses the same zval struct, but increments the refcounts and marks it as a reference.
1

Copy, unless you specify, in your case, &$dom in your function declaration.

UPDATE

In the OP, the example was an object. My answer was general and brief. Tomasz Struczyński provided an excellent, more detailed answer.

3 Comments

I think this is not exactly true, if the parameter is object.
My mistake. I was speaking more generally. In this case the OP example was indeed an object.
@TomaszStruczyński: It is always true. And the parameter cannot be "object" Objects are not values in PHP 5. In this case, it is an object reference.
0

in php5 objects pass by reference, in php4 and older - by value(copy) to pass by reference in php4 you must set & before object's name

Comments

0

It has nothing to do with function parameters. PHP 5 only has pointers to objects; it does not have "objects" as values. So your code is equivalent to this in C++:

MyDocumentHTMLDom *dom = new MyDocumentHTMLDom; myFun(dom); 

Now, many people mentioned pass by value or pass by reference. You didn't ask about this in the question, but since people mention it, I will talk about it. Like in C++ (since you mentioned you know C++), pass by value or pass by reference is determined by how the function is declared.

A parameter is pass by reference if and only if it has a & in the function declaration:

function myFun(&$dom) { ... } 

just like in C++:

void myFun(MyDocumentHTMLDom *&dom) { ... } 

If it does not, then it is pass by value:

function myFun($dom) { ... } 

just like in C++:

void myFun(MyDocumentHTMLDom *dom) { ... } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.