我对如何使用 & 运算符来减少内存感到非常困惑。
我可以回答下面的问题吗?
clase C{
function B(&$a){
$this->a = &$a;
$this->a = $a;
//They are the same here created new variable $this->a??
// Or only $this->a = $a will create new variable?
}
}
$a = 1;
C = new C;
C->B($a)
或者也许我的理解完全错误......
最佳答案
永远不要在 PHP 中使用引用来减少内存负载。 PHP 用它的内部 完美地处理了这个问题。写时复制机制。例子:
$a = str_repeat('x', 100000000); // Memory used ~ 100 MB
$b = $a; // Memory used ~ 100 MB
$b = $b . 'x'; // Memory used ~ 200 MB
如果您确切地知道自己在做什么并且需要它们来实现功能,则应该只使用引用(这几乎从来没有,所以您也可以忘记它们)。 PHP 引用很古怪,可能会导致一些意想不到的行为。
关于php - =& 运算符,内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14501017/