What is deep copy and shallow copy? How PHP uses them?

I got this question in an interview. Was aware of this conception and explained properly. But I think this topic is worth to explain, so that’s why this post is-

Shallow copy– In the process of shallow copying A, B will copy all of A’s field values. If the field value is a memory address it copies the memory address, and if the field value is a primitive type it copies the value of the primitive type. In layman’s term, shallow copy duplicates as minimum as possible. But the disadvantage is if you modify the memory address that one of B’s fields point to, you are also modifying what A’s fields point to.

Deep copy– In this process the data is actually copied completely. The advantage is that A and B do not depend on each other but this process is relatively slower and more expensive.
Continue reading

Advertisements