Php implements object cloning. Php object cloning method this article describes how php implements object cloning. Share it with you for your reference. The details are as follows :? 123456789101112131415161718 php object cloning method
This example describes how to clone objects in php. Share it with you for your reference. The details are as follows:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 |
// Defines the class staff, including the property id and name
Class staff
{
Private $ id;
Private $ name;
Function setID ($ id)
{
$ This-> id = $ id;
}
Function getID ()
{
Return $ this-> id;
}
Function setName ($ name)
{
$ This-> name = $ name;
}
Function getName ()
{
Return $ this-> name;
}
}
// Create a new staff object and initialize it
$ Ee1 = new staff ();
$ Ee1-> setID ("145 ");
$ Ee1-> setName ("Simon ");
// Clone a new object
$ Ee2 = clone $ ee1;
// Reset the ID value of the new object
$ Ee2-> setID ("146 ");
// Output ee1 and ee2
Echo "ee1 ID:". $ ee1-> getID ()."
";
Echo "ee1 Name:". $ ee1-> getName ()."
";
Echo "ee2 ID:". $ ee2-> getID ()."
";
Echo "ee2 Name:". $ ee2-> getName ()."
";
?>
|
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 |
// Defines the class staff, including the property id and name
Class staff
{
Private $ id;
Private $ name;
Function setID ($ id)
{
$ This-> id = $ id;
}
Function getID ()
{
Return $ this-> id;
}
Function setName ($ name)
{
$ This-> name = $ name;
}
Function getName ()
{
Return $ this-> name;
}
// Here is the _ clone function
Function _ clone ()
{
$ This-> id = $ this-> id + 1;
}
}
// Create a new staff object and initialize it
$ Ee1 = new staff ();
$ Ee1-> setID ("145 ");
$ Ee1-> setName ("Simon ");
// Clone a new object
$ Ee2 = clone $ ee1;
// Reset the ID value of the new object
// $ Ee2-& gt; setID ("146 ");
// Output ee1 and ee2
Echo "ee1 ID:". $ ee1-> getID ()."
";
Echo "ee1 Name:". $ ee1-> getName ()."
";
Echo "ee2 ID:". $ ee2-> getID ()."
";
Echo "ee2 Name:". $ ee2-> getName ()."
";
?>
|
I hope this article will help you with php programming.
Examples in this article describes how php implements object cloning. Share it with you for your reference. The details are as follows :? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18...