=&操作员,记忆

Mic*_*cah 2 php memory reference

关于如何使用&运算符来减少记忆,我感到非常困惑.

我能否回答以下问题?

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)
Run Code Online (Sandbox Code Playgroud)

或者也许我的理解是完全错误的.....

Fab*_*ler 12

永远不要仅仅使用PHP中的引用来减少内存负载.PHP 在写入机制完全处理其内部副本.例:

$a = str_repeat('x', 100000000); // Memory used ~ 100 MB
$b = $a;                         // Memory used ~ 100 MB
$b = $b . 'x';                   // Memory used ~ 200 MB
Run Code Online (Sandbox Code Playgroud)

如果您确切地知道自己在做什么并且需要它们来实现功能,那么您应该只使用引用(这几乎从不,所以您也可以忘掉它们).PHP引用是古怪的,可能会导致一些意外的行为.