Deep Clone

A deep clone is an object cloning, where the cloned object also clone its own object properties. This prevents the original object to share data with the cloned one, yet it is slower to process.

Deep cloning is achieved by using the __clone magic method. By default, PHP does shallow clone.

<?php

class Dog {
    private $name = '';
    private Address $address;

    function __construct(string $name, Address $address) {
        $this->address->setName($address);
    }

    function moves(string $address) {
        $this->address->setName($address);
    }

    function __clone() {
        $this->address = clone $this->address;
    }
}

$spike = new Dog('Spike', new Address('Acme city'));
$medor = clone $spike;
$medor->moves('Upstate farm');

// spike and medor do now share the same address

?>

Documentation

See also How to clone an object in PHP

Related : Magic Methods, __clone() method, Shallow Clone