开发者

php constructor

Sometimes I see this code in php :

$car = &am开发者_JS百科p; new Car();

instead of

$car = new Car();

What's the meaning of the ampersand sign?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜