PHP Object Clone Clone overview

Source: Internet
Author: User

PHP object replication, is a copy of the object's reference address, so use $objA = $objB this style, $objA and $OBJB will point to the same memory address. $objB can also be affected when the $obja is changed.

If we want to replicate $obja objects as $OBJB objects, what changes $objA after replication will not affect $OBJB. That is, $obja and $OBJB are two separate objects, but $OBJB's initial values are created by $obja, and the more efficient approach is to use the Clone () method.

$objB = Clone $objA;

The $objB value is an instance of the Clone () method changed in the $obja base plus the $obja case object.

When an object is copied, the references in all properties remain unchanged, point to the original variable, and if the __clone () method is defined, the __clone () method in the newly created object is invoked and can be used to modify the value of the property.

Example 1:clone Object

<?php  
class subclass{  
       
    private $name;  
    Private $age;  
       
    Public Function __construct () {  
        $this->name = ' fdipzone ';  
        $this->age = ' a ';  
    }  
       
    Public Function __clone () {  
        $this->name = ' xfdipzone ';  
        $this->age = ' i ';  
    }  
       
}  
       
       
Class myclass{public  
       
    $p 1;  
    Public $p 2;  
       
    Public Function __construct () {  
       
    } public  
       
    function __clone () {  
        $this->p1 = Clone $this->p1;  
    }  
       
}  
       
$obj = new MyClass ();  
$obj->P1 = new Subclass ();  
$obj->p2 = new Subclass ();  
       
$obj 2 = clone $obj;  
       
Echo ' <pre> ';  
Echo ' obj<br> ';  
Var_dump ($obj);  
Echo ' <br>obj2<br> ';  
Var_dump ($obj 2);  
echo ' OK ';  
Echo ' </pre> ';  
       
? >

The example above will output:

Obj  
object (MyClass) #1 (2) {  
  [P1]]=>  
  object (Subclass) #2 (2) {  
    [' name ': ' Subclass ':p rivate]=>  
    string (8) "Fdipzone"
    [' Age ': ' Subclass ':p rivate]=>  
    string (2) '
  }  
  [' P2 ']=>  
  object (Subclass) #3 (2) {  
    [] Name ': ' Subclass ':p rivate]=>  
    string (8) ' Fdipzone '
    [' age ': ' Subclass ':p rivate]=>  
    string (2) ' 30 '
  }  
}  
       
obj2  
object (MyClass) #4 (2) {  
  [' P1 ']=>  
  object (Subclass) #5 (2) {  
    [' name ' : "Subclass":p rivate]=>  
    string (9) "Xfdipzone"
    ["Age": "Subclass":p rivate]=>  
    string (2) "29"
  }  
  [' P2 ']=>  
  object (Subclass) #3 (2) {  
    [' name ': ' Subclass ':p rivate]=>  
    string (8) ' Fdipzone '
    [] Age ': ' Subclass ':p rivate]=>  
    string (2) '}  
}

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.