PHP object-oriented clone
One of the biggest disadvantages of php4 object-oriented is to regard objects as another data type, which makes many common OOP Methods unusable, such as design patterns. These OOP methods depend on passing objects as references to other classes, rather than passing objects as values. Fortunately, PHP solved this problem. All objects are considered as references by default. However, because all object pairs are regarded as references rather than values, it is more difficult to copy objects. If you try to copy an object, this will point to the address of the original object. To solve the replication problem, PHP provides a method to clone the Display object.
Example:
IntroductionCloneKeyword: Clone object:
name = $na; } function getName() { return $this->name; } function setNum($nu) { $this->num = $nu; } function getNum() { return $this->num; } } $test = new TestClone(); $test->setName("tianxin"); $test->setNum(123456); echo $test->getName(); echo $test->getNum().""; $test2 = clone $test; $test2->setName("liwei"); echo $test->getName(); echo $test->getNum().""; echo $test2->getName(); echo $test2->getNum(); ?>
Running result:
tian123456tian123456xia123456
From the running results, we can see that if test2 does not modify the name. Although the objects test and test2 have different attributes, changing the attributes of the test2 object does not affect the attributes of the test object, therefore, it can be determined that the clone is to pass the value, rather than a simple reference.
PHP5 defines a special method named "_ clone ()", which is automatically called during object cloning and uses "_ clone () the method creates an object with the same attributes and methods as the original object. To change the content of the original object after cloning () override the original attributes and methods. The "_ clone ()" method can have no parameters. It automatically contains two pointers: $ this and $ that,$ This pointsDuplicateAnd $ that pointsOriginal;
name = $na; } function getName() { return $this->name; } function setNum($nu) { $this->num = $nu; } function getNum() { return $this->num; } function __clone() { $this->name = "huang"; } } $test = new TestClone(); $test->setName("tian"); $test->setNum(123456); echo $test->getName(); echo $test->getNum().""; $test2 = clone $test;// $test2->setName("xia"); echo $test->getName(); echo $test->getNum().""; echo $test2->getName(); echo $test2->getNum(); ?>
Running result:
tian123456tian123456huang123456
Name = $ name; $ this-> sex = $ sex; $ this-> age = $ age;} // the way this person can speak, say your own property function say () {echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. "";} // method automatically called when the object is cloned. To change the content of the original object after cloning, you must rewrite the original attributes and methods in _ clone. Function _ clone () {// $ this refers to the copy p2, and $ that points to the original p1. In this way, the attributes of the copy are changed. $ This-> name = "I'm copying Michael Jacob $ that-> name"; // $ this-> age = 30 ;}} $ p1 = new Person ("Zhang San", "male", 20); $ p2 = clone $ p1; $ p1-> say (); $ p2-> say ();?>
Result After running:
My name is: Zhang San Gender: male my age is: 20 my name is: I'm copying Zhang San Gender: male my age is: 20