1

Update: in

http://php.net/manual/en/language.oop5.references.php

it says:

One of the key-points of PHP5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true.

Why is that? The following is a reason which I don't know is completely true or not:

I think loosely speaking, foo can be said to be an object, or an instance of the class Foo.

But is it true that very technically speaking, foo is just a reference, the exactly same way in Java and in Ruby, where variable foo is always just a reference to an object.

So that's why in PHP,

function add($obj) {
  $obj->a++;
}

We don't say "pass by reference", but very technically speaking, we are passing a value, which is a reference. So, it is "passing the reference", not "passing by reference".

But, if we say in PHP, that foo is an object, then I guess "passing by reference" can make sense. So is it true? foo is said to be a reference to an object, not an object, so that's why we are just "passing by value"?

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • 4
    *(related)* http://blog.golemon.com/2007/01/youre-being-lied-to.html – Gordon Oct 18 '10 at 10:37
  • the first block of code on that page that illustrates the idea is the same behavior in Java or Ruby, but we still call it a "reference to an object" in Java or Ruby, is it not? – nonopolarity Oct 18 '10 at 10:56
  • 1
    The related article is wrong. It says that PHP references are not references because they can be assigned to point to another object. References are references, a construct to represent a pointer to an object. Depending on the language specification, you can or cannot change a reference. C++ references cannot be changed. Java, Ruby and PHP references can be changed. – Vincent Robert Oct 18 '10 at 11:00
  • @Vincent While I am aware of the [fallacies of authority arguments](http://en.wikipedia.org/wiki/Argument_from_authority "Wikipedia: Argument from authority"), it should be noted that [Sara Golemon](http://devzone.zend.com/article/2999 "Profile of Sara Golemon on Devzone") is a PHP core and PECL contributor and has written [a book about writing PHP extensions](http://www.amazon.com/Extending-Embedding-PHP-Sara-Golemon/dp/067232704X), so I am inclined to believe she knows what she is writing about it. But feel free to discuss it with her. The comments in her blog are open. – Gordon Oct 18 '10 at 11:47
  • 1
    I do not doubt her knowledge of PHP, because every PHP code examples are real and behaviour is (simply) explained. What I disagree with is the wording of some sentences like `Notice now, that $b is still that original object. Had it been an actual reference with $a, it would have changed to a simple string as well.` This is wrong and based on a conception of references that are tied to C++. Java references for example works like PHP and are references... – Vincent Robert Oct 19 '10 at 01:06
  • I'm with Vincent... no one is "lying" about PHP, and those *really are* references... and they behave perfectly normally. Same as Python or C#. Except when you use the `ref` keyword...but that's something different. We probably need another word here to disambiguate. Like "alias". – mpen Nov 27 '10 at 07:41

3 Answers3

1

$foo is not an object, it is a reference. Saying that $foo is an object is an error, $foo is a reference that points to an object.

References are passed by value, like any other arguments in PHP (Java and Ruby also), so you cannot directly assign the reference to modify it, but you can work however you want on the object it points.

In order to simplify the abstraction, programmers sometimes say that "$foo is an object", this is wrong but it is easier than saying the whole "$foo is a reference that points to an object". In many cases, the difference does not actually matter except in some edge cases.

Vincent Robert
  • 35,564
  • 14
  • 82
  • 119
0

All objects are per default passed by reference in PHP 5+, before PHP 5 to pass by reference you used the "&" operator

Meaning that if you pass a object into a method the reference is copied, and another variable inside the method will contain a copy of the reference.

So you are not passing reference's by reference per default :), so you cant assign the variable containing the copy of the reference a new value and affect the original reference.

Mads Lee Jensen
  • 4,570
  • 5
  • 36
  • 53
  • it is because in the formal PHP docs: http://php.net/manual/en/language.oop5.references.php says One of the key-points of PHP5 OOP that is often mentioned is that "objects are passed by references by default". This is not completely true. – nonopolarity Oct 18 '10 at 10:43
  • 1
    What you describe in your answer in not pass-by-reference but pass-by-value of a reference. – Vincent Robert Oct 18 '10 at 10:52
0

PHP object variable does not actually contains an object (just as in many other languages) - it contains a "handle" of an object - i.e. some value that allows to identify the object, find it and manipulate it. This variable is passed around like any others - it can be passed by-val, by-ref, etc. However, if you copy this value to another variable, things happen both the same and different from what happens with integer or string.

Integer/String: value is copied, so when it's modified, the other one stays the same.

Object: value is copied, but the value is a handle. So if this handle is used to access the actual object (which is pretty much the only way to make use of this handle) and this access modifies it then the other variable - independent, but containing the same handle - would reflect the change.

So it is actually very consistent, just a bit not what you would expect treating "by value" and "by reference" naively - since what is being passed around is actually different for scalar and object variables. Object variables include a level of indirection.

This is, btw, why it almost never makes sense to pass object variable by-reference - unless you want to replace one object with another in the function.

StasM
  • 10,593
  • 6
  • 56
  • 103
  • this "handle" is what we usually call a "reference" or "pointer" – newacct Nov 07 '11 at 06:05
  • @newacct unfortunately, in PHP "reference" means something else, and it's not, strictly speaking, a pointer - it's an index in the object storage for specific object handler. – StasM Nov 08 '11 at 20:31