What is the difference between a 'shallow copy' and a 'deep copy' when cloning objects in PHP?
Last updated
A shallow copy (the default behavior of
clone) copies scalar properties but shares nested objects, so mutating a nested object shows up in both copies.A deep copy also clones nested objects, usually inside the
__clone()magic method that PHP calls automatically on the new copy.unserialize(serialize($obj))is a generic deep-copy shortcut, but it is slower and fails on resources and closures.