Is there any efficient method for object deep copy?

Source: Internet
Author: User
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 cloneObject Deep copyIs there any common and efficient method?
My practice is to use Reflection classTraverse, 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:

IncloneObjectDeep copyIs there any common and efficient method?
My practice is to useReflection classTraverse, 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.