Does PHP pass objects by value or by reference by default, and how does this impact memory usage?
Last updated
PHP passes objects by handle (a small identifier), so both variables point to the same instance and mutations are shared.
It is technically pass-by-value of the handle: reassigning the parameter to a new object does not change the caller's variable, unlike a true
&reference.Passing large objects is cheap since no deep copy happens. Use
clonefor an independent (shallow) copy.