0

Possible Duplicate:
In PHP (>= 5.0), is passing by reference faster?

I know the use of passing by reference. I'm wondering specifically is there some resource and/or performance benefit to passing by reference? Do I keep cleaner memory resources by passing, for instance PDO objects, by reference instead of value? I think php5 automatically passes objects by reference by default right?

Community
  • 1
  • 1
meteorainer
  • 913
  • 8
  • 21
  • I believe this question contains two different subjects. You can ask whether it's faster to pass a regular variable by value or by reference, but when passing objects you don't really have such choice. – Álvaro González Jun 20 '11 at 14:38
  • I don't understand how this question is an *exact duplicate* of the other question. Sure, he asks about performance, but he also asks about benefits of references in general. – netcoder Jun 20 '11 at 14:42
  • It's pretty close to the duplicate listed. Close enough that I would be comfortable closing this. Not exact, but very similar. Not sure how i didn't find that. Promise I did my searching :) – meteorainer Jun 21 '11 at 00:54

2 Answers2

2

Passing by reference is faster. PHP5 do pass objects by reference by default. I think under PHP 5.3, you still have to do $obj = &new Object();, but I could be wrong about that.

PHP5 do not pass array by reference. If you want to modify them in a function, you need to pass by reference.

Passing by value means that every single value is copied. For example, if you pass an array by value, it copies the array to a different memory location and every single element in it.

Pwnna
  • 9,178
  • 21
  • 65
  • 91
0

PHP References Explained and Objects and references might be of some help.

AlexV
  • 22,658
  • 18
  • 85
  • 122