Home >Backend Development >PHP Tutorial >PHP object-oriented cloning_PHP tutorial
One of the biggest disadvantages of PHP4 object-oriented is that it treats objects as another data type, which makes many common OOP methods unusable, such as design patterns. These OOP methods rely on passing objects to other class methods as references, rather than as values. Fortunately PHP solves this problem. All objects are now treated as references by default. But because all object pairs are treated as references rather than values, it is now harder to copy objects. If you try to copy an object, this will point to the address of the original object. To solve the duplication problem, PHP provides a method to clone the display object.
Examples are as follows:
First introduce the use of clone keyword to clone objects:
<!--?php class TestClone { public $name; public $num; function setName($na) { $this--->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 results:
tian123456 tian123456 xia123456
PHP5 defines a special method name "__clone()" method, which is automatically called when an object is cloned. Using the "__clone()" method will create an object with the same properties and methods as the original object. If If you want to change the content of the original object after cloning, you need to rewrite the original attributes and methods in __clone(). The "__clone()" method can have no parameters, and it automatically contains two pointers, $this and $that, $this points to the copy, while $that points to the original;
<!--?php class TestClone { public $name; public $num; function setName($na) { $this--->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(); ?>
tian123456 tian123456 huang123456
<!--?php class Person { // 下面是人的成员属性 var $name; // 人的名字 var $sex; // 人的性别 var $age; // 人的年龄 // 定义一个构造方法参数为属性姓名$name、性别$sex 和年龄$age 进行赋值 // function __construct($name="", $sex="",$age="") function __construct($name, $sex, $age) { $this--->name = $name; $this->sex = $sex; $this->age = $age; } // 这个人可以说话的方法, 说出自己的属性 function say() { echo "我的名字叫:" . $this->name . " 性别:" . $this->sex . " 我的年龄是:" . $this ->age . " "; } // 对象克隆时自动调用的方法, 如果想在克隆后改变原对象的内容,需要在__clone()中重写原来的属性和方法。 function __clone() { // $this 指的复本p2, 而$that 是指向原本p1,这样就在本方法里,改变了复本的属性。 $this->name = "我是复制的张三$that->name"; // $this->age = 30; } } $p1 = new Person ( "张三", "男", 20 ); $p2 = clone $p1; $p1->say (); $p2->say (); ?>
Result after running:
我的名字叫:张三 性别:男 我的年龄是:20 我的名字叫:我是复制的张三 性别:男 我的年龄是:20