Yes, you can implement __clone
and manually call clone
for variables that reference objects, but couldn’t PHP do this automatically using e.g. the is_object
, is_array
functions and other similar functions needed to check if we need to make a deep copy or not?
I’m not asking for opinions. I’m asking for good technical (maybe official) reasons why this function does not seem to exist. If this has been proposed, can you provide the link?
I suppose this could be related to circular references, which can cause problems, but such a builtin could e.g. raise an error if it detects a circular reference or do something else that could make sense, which maybe can be controlled by some parameter.
Python provides such as function deepcopy
, but I am also not super familiar with its implementation.
Please, do not state
- php’s
clone
does a shallow copy by default and that I can override__clone
to perform a deep copy - I can use
serialise
andunserialise
to perform a deep copy and this might not be a good idea for performance reasons - I can use a third party library to perform the deep copy
I’m only interested in the reasons why the people in charge of PHP have not yet decided to add a builtin deepcopy
function.