PHP Object-oriented cloning

Source: Internet
Author: User

One of the biggest drawbacks of PHP4 is that objects are treated as a different data type, which makes it impossible to use many common OOP methods, such as design patterns. These OOP methods rely on methods that pass objects as references to other classes, rather than passing them as values. Fortunately, PHP solves this problem. All objects are now considered references by default. But because all object pairs are treated as references instead of values, it is harder to replicate objects now. If you try to copy an object, this is the address that will point to the original object. To solve the replication problem, PHP provides a way to clone the display object.

Examples are as follows:

First, clone the object using the clone keyword:

<?PHPclassTestclone { Public  $name;  Public $num; functionSetName ($na)        {            $this->name =$na; }                functionGetName () {return $this-name; }                functionSetnum ($nu)        {            $this->num =$nu; }        functionGetnum () {return $this-num; }    }        $test=NewTestclone (); $test->setname ("Tianxin"); $test->setnum (123456); Echo $test-GetName (); Echo $test->getnum (). " <br> "; $test 2=Clone $test; $test 2->setname ("Liwei"); Echo $test-GetName (); Echo $test->getnum (). " <br> "; Echo $test 2-GetName (); Echo $test 2-Getnum (); ?>

Operation Result:

tian123456tian123456xia123456

From the running results we see that if Test2 does not modify the name. Although the objects of test and test2 are different objects but have the same properties, and changing the properties of the Test2 object does not affect the properties of the test object, it is possible to conclude that the clone is a pass value, not a simple reference.

PHP5 defines a special method named "__clone ()" method, which is the method that is called automatically when the object is cloned, and the "__clone ()" method will establish an object that has the same properties and methods as the original object, and if you want to change the contents of the original object after cloning, you need to __clone () Overriding the original properties and methods, the "__clone ()" method can have no parameters, it automatically contains $this and $that two pointers,$this point to the replica , and $that point to the original ;

<?PHPclassTestclone { Public  $name;  Public $num; functionSetName ($na)        {            $this->name =$na; }                functionGetName () {return $this-name; }                functionSetnum ($nu)        {            $this->num =$nu; }        functionGetnum () {return $this-num; }                function__clone () {$this->name = "Huang"; }    }        $test=NewTestclone (); $test->setname ("Tian"); $test->setnum (123456); Echo $test-GetName (); Echo $test->getnum (). " <br> "; $test 2=Clone $test;//$test 2->setname ("Xia");    Echo $test-GetName (); Echo $test->getnum (). " <br> "; Echo $test 2-GetName (); Echo $test 2-Getnum (); ?>

Operation Result:

tian123456tian123456huang123456

Look at an example:

<?PHPclassPerson {//The following is the person's member property    var $name;//Man's name    var $sex;//Man's Sex    var $age;//man's age//define a constructor method parameter assigns a value to the attribute name $name, gender $sex, and age $age/function __construct ($name = "", $sex = "", $age = "")    function__construct ($name,$sex,$age) {        $this->name =$name; $this->sex =$sex; $this->age =$age; }    //This person can speak in a way that speaks his own attributes    functionsay () {Echo"My name is:".$this->name. "Gender:".$this->sex. "My Age is:".$this->age. "<br>"; }    //The method that is called automatically when the object is cloned, and if you want to change the contents of the original object after cloning, you need to override the original properties and Methods in __clone ().     function__clone () {//$this refers to the replica P2, while $that is pointing to the original P1, so that in this method, the properties of the replica are changed.         $this->name = "I am a copy of Zhang San$that->name "; //$this->age =;    }}$p 1=NewPerson ("Zhang San", "male", 20 );$p 2=Clone $p 1;$p 1-say ();$p 2-say ();?>

Operation Result:

My name is: Zhang San Sex: Male my age is: My name is: I was copied Zhang San sex: Male my age is:20

PHP Object-oriented cloning

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.