Clone object in PHP

Source: Internet
Author: User
Sometimes we need to use two or more of the same objects in a project, if you re-create the object using the "new" keyword, then assign the same attributes, it is cumbersome and error-prone, so it is necessary to completely clone an object exactly as it is, And after cloning, two objects do not interfere with each other.

In PHP4 we use the "clone" keyword to clone the object;

01

02 classPerson

03 {

04 //下面是人的成员属性

05 var$name; //人的名子

06 var$sex; //人的性别

07 var$age; //人的年龄

08

09 //定义一个构造方法参数为属性姓名$name、性别$sex和年龄$age进行赋值

10 function__construct($name= "", $sex= "", $age= "") {

11 $this->name=$name;

12 $this->sex=$sex;

13 $this->age=$age;

14 }

15

16 //这个人可以说话的方法,说出自己的属性

17 functionsay() {

18 echo"我的名子叫:". $this->name ." 性别:". $this->sex ." 我的年龄是:". $this->age ."
"
;

19 }

20 }

21

22 $p1= newPerson("张三","男", 20);

23

24 //使用“clone”克隆新对象p2,和p1对象具有相同的属性和方法。

25 $p2=clone $p1;

26 $p2->say();

27 ?>

PHP4 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 ;

01

02 classPerson

03 {

04 //下面是人的成员属性

05 var$name; //人的名子

06 var$sex; //人的性别

07 var$age; //人的年龄

08

09 //定义一个构造方法参数为属性姓名$name、性别$sex和年龄$age进行赋值

10 function__construct($name= "", $sex= "", $age= "") {

11 $this->name=$name;

12 $this->sex=$sex;

13 $this->age=$age;

14 }

15

16 //这个人可以说话的方法, 说出自己的属性

17 functionsay() {

18 echo"我的名子叫:". $this->name ." 性别:". $this->sex ." 我的年龄是:". $this->age ."
"
;

19 }

20

21 //对象克隆时自动调用的方法, 如果想在克隆后改变原对象的内容,需要在__clone()中重写原本的属性和方法

22 function__clone() {

23

24 //$this指的复本p2, 而$that是指向原本p1,这样就在本方法里,改变了复本的属性。

25 $this->name ="我是假的 $that->name";

26 $this->age = 30;

27 }

28

29 }

30

31 $p1= newPerson("张三","男", 20);

32 $p2= clone$p1;

33 $p1->say();

34 $p2->say();

35 ?>

The above example outputs:

My name is called: Zhang San Sex: Male my age is: 20
My name is called: I am false zhang San Sex: Male my age is:

The above describes the clone object in PHP, including the contents of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.