可以在對(duì)象類(lèi)中定義一個(gè)__clone()方法來(lái)調(diào)整對(duì)象的克隆行為。此方法的代碼將在克隆操作期間執(zhí)行。除了將所有現(xiàn)有對(duì)象成員復(fù)制到目標(biāo)對(duì)象之外,還會(huì)執(zhí)行__clone()方法指定的操作。下面修改Corporate_Drone類(lèi),增加以下方法:
1
2
3
|
function __clone() { $this ->tiecolor = "blue" ; } |
之后,創(chuàng)建一個(gè)新的Corporate_Drone對(duì)象,增加employeeid成員的值,克隆這個(gè)對(duì)象,然后輸出一些數(shù)據(jù),從而顯示克隆對(duì)象的tiecolor確實(shí)是通過(guò)__clone()方法設(shè)置的。示例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?php // Create new corporatedrone object $drone1 = new corporatedrone(); // Set the $drone1 employeeid member $drone1 ->setEmployeeID( "12345" ); // Clone the $drone1 object $drone2 = clone $drone1 ; // Set the $drone2 employeeid member $drone2 ->setEmployeeID( "67890" ); // Output the $drone1 and $drone2 employeeid members echo "drone1 employeeID: " . $drone1 ->getEmployeeID(). "<br />" ; echo "drone2 employeeID: " . $drone2 ->getEmployeeID(). "<br />" ; echo "drone2 tiecolor: " . $drone2 ->getTiecolor(). "<br />" ; ?> |
程序運(yùn)行結(jié)果
1
2
3
|
drone1 employeeID: 12345 drone2 employeeID: 67890 drone2 tiecolor: |
再來(lái)一個(gè)小例子:
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
|
<?php class Fruit { private $name = "水果" ; private $color = "顏色" ; public function setName( $name ){ $this ->name = $name ; } public function setColor( $color ){ $this ->color = $color ; } function showColor(){ return $this ->color. '的' . $this ->name. "<br />" ; } function __destruct(){ echo "被吃掉了(對(duì)象被回收) <br />" ; } } $apple = new Fruit(); $apple ->setName( "大蘋(píng)果" ); $apple ->setColor( "紅色" ); echo $apple ->showColor(); $clone_apple = $apple ; $clone_apple ->setName( "小蘋(píng)果" ); $clone_apple ->setColor( "青色" ); echo $clone_apple ->showColor(); ?> |
上面只是將一個(gè)類(lèi)賦值給另一個(gè)類(lèi),所以此時(shí)內(nèi)存中仍是一個(gè)對(duì)象。
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
|
<?php class Fruit { private $name = "水果" ; private $color = "顏色" ; public function setName( $name ){ $this ->name = $name ; } public function setColor( $color ){ $this ->color = $color ; } function showColor(){ return $this ->color. '的' . $this ->name. "<br />" ; } function __destruct(){ echo "被吃掉了(對(duì)象被回收) <br />" ; } function __clone(){ $this ->name = "克隆水果" ; } } $apple = new Fruit(); $apple ->setName( "大蘋(píng)果" ); $apple ->setColor( "紅色" ); echo $apple ->showColor(); $clone_apple = clone $apple ; $clone_apple ->setColor( "青色" ); echo $clone_apple ->showColor(); ?> |
clone方法克隆出了一個(gè)新的類(lèi),所以此時(shí)內(nèi)存中有兩個(gè)對(duì)象。
php的__clone()方法對(duì)一個(gè)對(duì)象實(shí)例進(jìn)行的淺復(fù)制,對(duì)象內(nèi)的基本數(shù)值類(lèi)型進(jìn)行的是傳值復(fù)制,而對(duì)象內(nèi)的對(duì)象型成員變量,如果不重寫(xiě)__clone方法,顯式的clone這個(gè)對(duì)象成員變量的話,這個(gè)成員變量就是傳引用復(fù)制,而不是生成一個(gè)新的對(duì)象.如下面一個(gè)例子的第28行注釋所說(shuō)
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
|
<?php class Account { public $balance ; public function __construct( $balance ) { $this ->balance = $balance ; } } class Person { private $id ; private $name ; private $age ; public $account ; public function __construct( $name , $age , Account $account ) { $this ->name = $name ; $this ->age = $age ; $this ->account = $account ; } public function setId( $id ) { $this ->id = $id ; } public function __clone() { #復(fù)制方法,可在里面定義再 clone 是進(jìn)行的操作 $this ->id = 0; $this ->account = clone $this ->account; #不加這一句,account在 clone 是會(huì)只被復(fù)制引用,其中一個(gè)account的balance被修改另一個(gè)也同樣會(huì)被修改 } } $person = new Person( "peter" , 15, new Account(1000)); $person ->setId(1); $person2 = clone $person ; $person2 ->account->balance = 250; var_dump( $person , $person2 ); ?> |
輸出:
object(Person)#1 (4) { ["id":"Person":private]=> int(1) ["name":"Person":private]=> string(5) "peter" ["age":"Person":private]=> int(15) ["account"]=> object(Account)#2 (1) { ["balance"]=> int(1000) } } object(Person)#3 (4) { ["id":"Person":private]=> int(0) ["name":"Person":private]=> string(5) "peter" ["age":"Person":private]=> int(15) ["account"]=> object(Account)#4 (1) { ["balance"]=> int(250) } }