Home > Backend Development > PHP Tutorial > clone object in php

clone object in php

WBOY
Release: 2016-07-30 13:30:37
Original
1259 people have browsed it

Sometimes we need to use two or more identical objects in a project. If you use the "new" keyword to re-create the object and then assign the same attributes, this is more cumbersome and error-prone. , so it is very necessary to completely clone an identical object from an object, and after cloning, the two objects will not interfere with each other.

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

02

class
Person

03
{
04

​​
//The following are the member attributes of people

05var//The person’s name
$name;

06//person Age
                                                                                       var$age;

0809

​​ //Define a constructor parameter as the attribute name $name, gender $sex and age $age are assigned function
10
__construct(

$name= , $sex =
""
""

, $age""
= ) {

11                                                                                     ;name=
$name;12                         $this->sex= $sex;

13                                                                                             }
14​​​

15 ​​​//This person can speak in a way that tells his own attributes

17
functionsay() {
18

                                                                                                            The son called: "$this
. $this ->name . " Gender: ".
->sex .

" My age is: "

. ;
$this->age . "
"

}
19

20}

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

23
24

//Clone using "clone" The new object p2 has the same properties and methods as the p1 object.
25

$p2
=clone $p1 ;

26 $p2->say();

27 ?>

PHP4 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 attributes and methods as the original object. If you want to 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. It automatically contains two pointers, $this and $that, and $this points to The copy , and $that points to the original;

01 <?

02classPerson

03{

04 //The following are the member attributes of people

06
05 var$name;

var 07
$sex; //Sex of a person

var08
$age; //Age of a person

//Define a constructor parameter as attributes name $name, gender $sex and age $age is assigned

10function__construct(

$name

= ""$sex;
, = "", $age= "") {11                                                                                                                                      $name

12 $sex;

13
14 }

1516

//The way this person can speak is to tell his own attributes

17

function

say() {

18

->name .
echo"My name is:" . $this
" Gender: "

.

$this;
->sex . " My age is: " . $this->age . "
"

19 } 20 21

//Method automatically called when an object is cloned. If you want to change the content of the original object after cloning, you need to rewrite the original attributes and methods in __clone()

22

function

__clone() {

23

24             //$this refers to the copy p2, and $that points to the original p1, so in this method, the attributes of the copy are changed.

25 $this->name = "I am fake $that->name";

26 ->age = 30;

27

29
}

30$p1newPerson(
=
"张三"

, "male"
, 20);

32$p233
= clone $p1;
$p1

->say();$p2->say();
34

?>
35

My name is: I am the fake Zhang San Gender: Male My age is: 30
Output of the above example: Mine My name is: Zhang San Gender: Male My age is: 20 The above introduces the clone object in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template