What is the difference between a 'shallow copy' and a 'deep copy' when cloning objects in PHP?

Last updated 15 June 2026

  • 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.

  • https://interview.sughara.in/post/advanced-php-interview-questions-2025
  • https://dev.to/jahid6597/beyond-cloning-sculpting-javascript-mirrors-with-the-art-of-shallow-copy-and-the-depth-of-deep-copy-3p64
All PHP interview questions