کدهای زیر نمونه ای از deep clone هست:
با استفاده از متد
public function __clone()
{
#
}
پراپرتی هایی که خودشان هم کلاس یک شی از یک کلاس هستن رو معرفی کرد تا موقع clone اونها رو هم بشناسه.
مثال:
<?php
class Address
{
public $street;
public $city;
}
class Person
{
public $name;
public $address;
public function __construct($name)
{
$this->name = $name;
$this->address = new Address();
}
public function __clone()
{
$this->address = clone $this->address;
}
}
$bob = new Person('Bob');
$bob->address->street = 'North 1st Street';
$bob->address->city = 'San Jose';
$alex = clone $bob;
$alex->name = 'Alex';
$alex->address->street = '1 Apple Park Way';
$alex->address->city = 'Cupertino';
var_dump($bob);
var_dump(
object(Person)#1 (2) {
["name"]=> string(3) "Bob"
["address"]=> object(Address)#2 (2) {
["street"]=> string(16) "North 1st Street"
["city"]=> string(8) "San Jose"
}
}
object(Person)#3 (2) {
["name"]=> string(4) "Alex"
["address"]=> object(Address)#4 (2) {
["street"]=> string(16) "1 Apple Park Way"
["city"]=> string(9) "Cupertino"
}
}