0

What is the exact purpose of a statement like this?

$myObj =& $existingObject

Is this extending $myObj with the properties and methods of $existingObject?

What does the equals sign (=) and the ampersand (&) married together here do?

random
  • 9,774
  • 10
  • 66
  • 83
david
  • 3
  • 3
  • 2
    You're title and your question don't match up, is it `&=` or `=&`? – Chris Eberle May 18 '11 at 21:58
  • possible duplicate of [Reference assignment operator in php =&](http://stackoverflow.com/questions/1768343/reference-assignment-operator-in-php) – Pang Jul 16 '15 at 02:08

4 Answers4

8

Um first of all, &= and =& are two different things. Which is it?

  • &= is a bitwise and with the righthand side
  • =& is better written as = & (with a space), is assigning something as a reference.
Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
  • So by using =& can I give one object access to another objects methods as if it were part of the same class? – david May 18 '11 at 22:13
  • No, by using `=&` you now have two variables pointing to the same thing. So if I do `$a = new something(); $b = &$a;` Both `$a` and `$b` now point to the exact same instance of `something`. – Chris Eberle May 18 '11 at 22:15
  • Thats what I though although I have a situation right now in a codeigniter project where a code example assigns one object to another by reference as in my example. One object seems to have extended the other and has access to its methods whilst still retaining access to its own. Its quite confusing – david May 18 '11 at 22:17
2

Based on the names of the variables, they are objects. Objects are always passed by reference, so = & would be redundant.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
1

$myObj becomes a reference to $existingObject instead of being copied. So any change to $myObj also changes $existingObject. (see this article)

Doug T.
  • 64,223
  • 27
  • 138
  • 202
1

This is a pass by reference. It means anything you do to $existingObject will also be done to $myObj because one is a reference to the other.

Jage
  • 7,990
  • 3
  • 32
  • 31