简体   繁体   English

PHP构造函数

[英]php constructor

Sometimes I see this code in php : 有时我在php中看到以下代码:

$car = & 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; &通过引用分配-即,如果您说$a =& $b; , then $a and $b refer to the exact same thing, and changing one will change the other. ,则$a$b指的是完全相同的事物,而更改一个将更改另一个。

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. 它与类似new用法大部分是PHP4的遗留物-那时,PHP曾在分配对象时复制对象,并且在执行诸如DOM操作之类的操作时可能会导致大问题。 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. PHP5中的对象很少(我认为永远不需要)(因为现在无论如何都引用了对象变量),而且我认为它甚至被弃用了。

This assigns by reference instead of by value. 这是通过引用而不是按值分配的。 Ie, 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." “自PHP 5起,new会自动返回引用,因此不建议在此上下文中使用=&并生成E_STRICT消息。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM