1

Well, I read in my handy PHP book that it's very important to be able to distinguish between reference and variable parameters. The book says that the original value of parameterized variables are preserved when the variable is changed, and the original values of parameterized references change when the reference is changed. It says that's the key difference, if I am reading right.

Well, I'm wondering when each is more useful than the other. How do I know when to use variables and when to use references when I create my own functions?

4
  • 1
    Actually objects are passed around by reference by default as of 5.3 (though this does not affect primitives). Commented Jun 17, 2011 at 15:19
  • 1
    @Tomalak: To be more specific, objects are passed (and assigned) via object identifier which has slightly different behavior than a reference. For instance, if you overwrite a passed object by assigning it the string value of 'abc', you'll see the original object remains unchanged. Commented Jun 17, 2011 at 15:25
  • @webbiedave: Sure, that. Commented Jun 17, 2011 at 15:26
  • I think I'd have to see the code or the outputs or something. I can't be sure I understand, really. Commented Jun 17, 2011 at 16:05

2 Answers 2

3

It's pretty straightforward. Use references when you need to modify the value of the variable passed in to the function. Use variables when you don't need to or want to modify the value.

So, for example, if you're writing a function that takes an array and changes that array, you'd be better off using a reference for that array rather than returning a new array from the function.

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

3 Comments

Don't [explicitly] use references when dealing with objects. And don't do nothing when you want to copy an object on receipt into a function.
I'm sorry, this is way above my head. I've only just read about references and variables and don't see how they are related to objects. Maybe I'll have to come back at a later time to understand. Here's my thinking, though, if it helps you to explain: objects don't seem to have return values. So, how are they copied into functions?
@Fohsap they're passed by reference.
2

"References" (variable aliases) make your code harder to understand and could be a source of hard to follow errors. There are no valid reasons to use references in php and to be on the safer side try to avoid them altogether.

And no, objects in php5 have nothing to do with "references".

"References" as implemented in php is a strange concept. Normally, in programming languages variables are independent of each other so that changing one variable doesn't affect others. Php "references" allow several variables to share the same value and to be dependent of each other. Basically, you change one variable, and suddenly another one, which you think is totally unrelated, is getting changed too. It's no good thing and often leads to much confusion.

Objects in php (do I need to add 'five'?) have nothing to do with "references" in the above sense. They behave much like C pointers (actually, this is what they are under the hood) - when you pass an object to a function, you actually pass a pointer, and the function can use this pointer to manipulate the object contents, but there's no way for the function to change the passed variable itself, for example, make it point to another object.

This "objects are references" misunderstanding is probably because people confuse php "references" (ampersand syntax) with the generic CS term , which also applies to pointers, handles etc.

6 Comments

I think you should expand on your statement that "There are no valid reasons to use references in php."
I agree.... Meanwhile, I didn't really understand how objects and references could be related. I thought references were assigned to functions, not objects.... I'm really interested in hearing more, but I guess it's hard to explain through a void.... If you could expand on your answer, I would really appreciate it.
Thanks. It makes a lot more sense because of the C-level explanation. This is the way it was drawn up in my book--objects are pointers. I still don't fully understand, but I am pretty confident it's over my head at this point. It seems to me, though, that the occasion to use references would be one in which a variable change should effect the return value for multiple functions, the same reference should be used as a parameter for each of these functions....
What I'm understanding, after reading into C a little, is that when a variable is referenced by its name, a copy is made. So, you can change the value of the copy without changing the original value. On the other hand, when you reference a variable by reference, only a pointer is used. Because there is no copy, only the original value can be changed. If the original value is changed, the value of the references are also changed because they all lead back to the original. So, reference = one value possible; name = multiple values possible.
@stereofrog: "there's no way for the function to change the passed variable itself, for example, make it point to another object." I'm not sure what you mean by that. You can pass an object to a function by reference and then assign the passed variable to another object. When the caller manipulates the variable from then on, it will be "pointing" to the new object. codepad.org/8I8hVFoz
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.