可以在物件類別中定義一個clone()方法來調整物件的複製行為。此方法的程式碼將在克隆操作期間執行。除了將所有現有物件成員複製到目標物件之外,還會執行clone()方法指定的操作。下面修改Corporate_Drone類,增加以下方法:
function clone() { $this->tiecolor = "blue"; }
之後,建立一個新的Corporate_Drone對象,增加employeeid成員的值,克隆這個對象,然後輸出一些數據,從而顯示克隆對象的tiecolor確實是透過clone()方法設定的。範例程式碼:
<?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 />"; ?>
程式運行結果
drone1 employeeID: 12345 drone2 employeeID: 67890 drone2 tiecolor:
再來一個小範例:
<?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 "被吃掉了(对象被回收) <br />"; } } $apple = new Fruit(); $apple->setName("大苹果"); $apple->setColor("红色"); echo $apple->showColor(); $clone_apple = $apple; $clone_apple->setName("小苹果"); $clone_apple->setColor("青色"); echo $clone_apple->showColor(); ?>
上面只是將一個類別賦值給另一個類,所以此時記憶體中仍是一個對象。
<?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 "被吃掉了(对象被回收) <br />"; } function clone(){ $this->name = "克隆水果"; } } $apple = new Fruit(); $apple->setName("大苹果"); $apple->setColor("红色"); echo $apple->showColor(); $clone_apple = clone $apple; $clone_apple->setColor("青色"); echo $clone_apple->showColor(); ?>
clone方法克隆出了一個新的類,所以此時記憶體中有兩個物件。
php的clone()方法對一個物件實例進行的淺複製,物件內的基本數值類型進行的是傳值複製,而物件內的物件型成員變數,如果不重寫clone方法,顯式的clone這個物件成員變數的話,這個成員變數就是傳引用複製,而不是產生一個新的物件.如下面一個例子的第28行註釋所說
<?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() { #复制方法,可在里面定义再clone是进行的操作 $this->id = 0; $this->account = clone $this->account; #不加这一句,account在clone是会只被复制引用,其中一个account的balance被修改另一个也同样会被修改 } } $person = new Person("peter", 15, new Account(1000)); $person->setId(1); $person2 = clone $person; $person2->account->balance = 250; var_dump($person, $person2); ?>
淺克隆:只是克隆物件中的非物件非資源數據,即物件中屬性儲存的是物件類型,則會出現克隆不完全
<?php class B{ public $val = 10; } class A{ public $val = 20; public $b; public function construct(){ $this->b = new B(); } } $obj_a = new A(); $obj_b = clone $obj_a; $obj_a->val = 30; $obj_a->b->val = 40; var_dump($obj_a); echo '<br>'; var_dump($obj_b);
運行結果如下:
object(A)[1] public 'val' => int 30 public 'b' => object(B)[2] public 'val' => int 40 object(A)[3] public 'val' => int 20 public 'b' => object(B)[2] public 'val' => int 40
深克隆:一個物件的所有屬性資料都徹底的複製,需要使用魔術方法clone(),並在裡面實作深度複製
<?php class B{ public $val = 10; } class A{ public $val = 20; public $b; public function construct(){ $this->b = new B(); } public function clone(){ $this->b = clone $this->b; } } $obj_a = new A(); $obj_b = clone $obj_a; $obj_a->val = 30; $obj_a->b->val = 40; var_dump($obj_a); echo '<br>'; var_dump($obj_b);
運行結果如下:
object(A)[1] public 'val' => int 30 public 'b' => object(B)[2] public 'val' => int 40 object(A)[3] public 'val' => int 20 public 'b' => object(B)[4] public 'val' => int 10
以上是php__clone()克隆方法以及淺克隆和深克隆使用實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!