0

Sometimes I see this code in php :

$car = & new Car();

instead of

$car = new Car();

What's the meaning of the ampersand sign?

2 Answers 2

3

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.

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

Comments

1

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."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.