Reputation: 1
Sometimes I see this code in php :
$car = & new Car();
instead of
$car = new Car();
What's the meaning of the ampersand sign?
Upvotes: 0
Views: 296
Reputation: 86505
The & assigns by reference -- that is, if you say $a =& $b;
, then $a
and $b
refer to the exact same thing, and changing one will change the other.
Its use with new
like this, is mostly a leftover from PHP4 -- back then, PHP used to copy objects when assigning them, and that could cause big problems when you are doing stuff like DOM manipulation. It's rarely (i think never) required with objects in PHP5 (since now object variables are references anyway), and i think it's even considered deprecated.
Upvotes: 3
Reputation: 53563
This assigns by reference instead of by value. I.e., you get a pointer to the object instead of a copy of it. Note:
"Since PHP 5, new returns a reference automatically, so using =& in this context is deprecated and produces an E_STRICT message."
Upvotes: 1