Is there any common and efficient way to use deep copy when cloning objects? My practice is to use the reflection class to traverse, but it is only a layer. Is it true that the objects in the object are still referenced in this way? Can anyone provide multi-layer Deep copy? {Code...} in
clone
Object
Deep copy
Is there any common and efficient method?
My practice is to use
Reflection class
Traverse, but only one layer. Is this true?
Objects in the objectStill referenced?
Can anyone provide
Multi-layer Deep copy?
public function __clone(){ $reflectClass = new \ReflectionClass(__CLASS__); foreach ($reflectClass->getProperties() as $properity) { $name = $properity-> name; if (\is_object($this->$name)) $this->$name = clone $this->$name; }}
Reply content:
Inclone
ObjectDeep copy
Is there any common and efficient method?
My practice is to useReflection class
Traverse, but only one layer. Is this true?Objects in the objectStill referenced?
Can anyone provideMulti-layer Deep copy?
public function __clone(){ $reflectClass = new \ReflectionClass(__CLASS__); foreach ($reflectClass->getProperties() as $properity) { $name = $properity-> name; if (\is_object($this->$name)) $this->$name = clone $this->$name; }}
Reflection is not required. A single object only needs to concern about which attributes need to be copied in depth and directly calls clone copy, as for how the object handles his deep copy, it should be handed over to him for processing.