5

There is a comment on an answer on SO:

Code in answer:

$larry = & $fred;

Comment:

This is deprecated and generates a warning

But I don't think it is correct. Or is it?

So basically the question is:

can I copy a variable to another variable by reference?

Community
  • 1
  • 1
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • I do think yes... $larry = $fred should copy your variable in the other without a problem... but with something that simple I must be misunderstanding the question. – Fredy31 Nov 25 '11 at 21:15
  • @Fredy31: It's simple because it is an example. :-) I don't think there is really the need for full length code to get an answer :D – PeeHaa Nov 25 '11 at 21:17
  • @PeeHaa: can you post the full warning message – Drahcir Nov 25 '11 at 21:18
  • @RichardLivingston: what do you mean? What warning message? – PeeHaa Nov 25 '11 at 21:25
  • 1
    It is not. It is forcibly passing function parameters per reference that is gone (fatal error). There is a warning for assigning `& new` however. `Deprecated: Assigning the return value of new by reference is deprecated` (but that was the case in 5.3 already) – mario Nov 25 '11 at 21:46
  • @PeeHaa: I just meant the warning message that php gives, you said "This is deprecated and generates a warning" and I wanted to see it. Never mind I see you already selected BA on this – Drahcir Dec 02 '11 at 18:05

3 Answers3

7

Depends on what type variable $fred is. If it's an object, it will be passed as reference a pointer to the object (thanks NikiC) will be passed as value anyway as of PHP 5, so there is no need to explicitly do it. Thus far, the comment is correct-ish.

For all other variable types, however, passing by reference needs to be explicitly specified and is not deprecated. Although it could be argued that it's usually not good programming practice to use a lot of references in PHP.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • "There is no need to do this" -- I don't think that statement is true. In most cases, you don't need to, because it doesn't consume any more memory by doing so, yes, but if you want to alias a variable, this still has good uses. – mpen Nov 25 '11 at 21:21
  • @Mark there is no need to pass objects explicitly by reference because that's what already happens automatically. That's what the commenter meant. It's only correct for objects, though. – Pekka Nov 25 '11 at 21:22
  • `Although it could be argued that it's usually not good programming practice to do so`. The opinions seem to vary on that topic. However I do see this risk of doing it. +1 for getting the object part out of it. Didn't see it was about objects. – PeeHaa Nov 25 '11 at 21:24
  • @Pekka: Good point. I was forgetting how PHP behaved. Updated my answer with examples. – mpen Nov 25 '11 at 21:30
  • 3
    Objects are passed in a reference-like behavior since PHP 5 (meaning that a pointer to the object is passed by value). This is (slightly) different from passing by reference. Also it will not throw a notice. The only things that can throw a notice in 5.3 are call time pass by ref and $var =& new X. – NikiC Nov 25 '11 at 21:52
2

Pretty sure it's not deprecated. I've been doing things like that a lot in PHP 5.3, basically to alias a deeply nested array. For example:

$short = &$a['really']['deep']['array']

When you do $a = $b and $a is an object it creates a "reference". This is best illustrated with an example:

>> $o = new stdClass()
stdClass::__set_state(array(
))
>> $o->a = 5
5
>> $o
stdClass::__set_state(array(
   'a' => 5,
))
>> $o2 = $o
stdClass::__set_state(array(
   'a' => 5,
))
>> $o2->a = 6
6
>> $o
stdClass::__set_state(array(
   'a' => 6,
))

Note that both $o->a and $o2->a are now 6, even though we only did the assignment on one of them.

When $a is a primitive (or scalar in PHP) such as string, int or float, it behaves a bit differently; the variable is copied:

>> $a = 1
1
>> $b = $a
1
>> $b = 2
2
>> $a
1

Note that $a is still 1.

When you use the ampersand, however, it basically works as an alias. Any changes to one variable will effect the other.

>> $c = 1
1
>> $d = &$c
1
>> $d = 2
2
>> $c
2

Note that $c is also 2 now.

mpen
  • 272,448
  • 266
  • 850
  • 1,236
0

This might help: How does the '&' symbol in PHP affect the outcome?

Community
  • 1
  • 1
j3ffz
  • 715
  • 1
  • 6
  • 17