PHP References

Something you should be aware of, if you are cloning objects containing objects:
Objects are always passed as reference, so the idea of cloning the object when returning is the first thing to do if you want to avoid changing the value because you are operating on a reference. The issue is that the clone keeps the child references.

To clone the childs you need to override __clone(). So adding this method in the example class will prevent this behavior:

Share This: