A Complete Guide to the “clone” Keyword in PHP

The “clone” keyword in PHP is used to create a copy of an object. When an object is cloned, a new object is created with a copy of the original object’s properties and methods. The new object is independent of the original object, meaning that changes made to the new object will not affect the original object.

Cloning objects

To clone an object in PHP, you can use the “clone” keyword followed by the object variable. This will create a new object that is a copy of the original object.

For example:

class Animal {
  public $name;
  public $age;
}

$animal1 = new Animal();
$animal1->name = "Fluffy";
$animal1->age = 5;

$animal2 = clone $animal1;

echo $animal2->name; // outputs "Fluffy"
echo $animal2->age; // outputs 5

In this example, a class called “Animal” is defined with two public properties called “name” and “age”. An object of the “Animal” class called “$animal1” is created and its properties are set. Another object called “$animal2” is created by cloning “$animal1”. The “$animal2” object has a copy of the “$animal1” object’s properties and values.

Cloning objects with constructors

If the class that you are cloning has a constructor method (a special method that is called when an object is created), the constructor method will not be called when the object is cloned. This means that any initialisation code in the constructor will not be executed when the object is cloned.

For example:

class Animal {
public $name;
public $age;

public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
}

$animal1 = new Animal("Fluffy", 5);

$animal2 = clone $animal1;

echo $animal2->name; // outputs "Fluffy"
echo $animal2->age; // outputs 5

In this example, the “Animal” class has a constructor method that sets the “name” and “age” properties of the object. An object called “$animal1” is created using the constructor and its properties are set. Another object called “$animal2” is created by cloning “$animal1”. The “$animal2” object has a copy of the “$animal1” object’s properties and values, but the constructor was not called when “$animal2” was created.

Cloning objects with magic methods

PHP has a set of “magic methods” that are special methods that are called automatically by the PHP runtime in certain circumstances. One of these magic methods is called “__clone”, which is called when an object is cloned.

You can use the “__clone” magic method to customise the cloning process for an object. For example, you can use it to reset certain properties or perform other tasks when the object is cloned.

To define the “__clone” magic method, you need to use the “function” keyword followed by “__clone” and a set of parentheses.

For example:

class Animal {
  public $name;
  public $age;

  public function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
  }

  public function __clone() {
    $this->name = "Copy of " . $this->name;
  }
}

$animal1 = new Animal("Fluffy", 5);

$animal2 = clone $animal1;

echo $animal2->name; // outputs "Copy of Fluffy"
echo $animal2->age; // outputs 5

In this example, the “Animal” class has a “__clone” magic method that modifies the “name” property of the object when it is cloned. An object called “$animal1” is created using the constructor and its properties are set. Another object called “$animal2” is created by cloning “$animal1”. The “$animal2” object has a copy of the “$animal1” object’s properties and values, and the “__clone” magic method was called when “$animal2” was created, which modified the “name” property.

Cloning objects with object references

It’s important to note that when you clone an object in PHP, any object references in the original object will not be copied to the new object. Instead, the new object will have its own references to the same objects as the original object.

For example:

class Animal {
  public $name;
  public $age;
}

class Dog {
  public $animal;

  public function __construct(Animal $animal) {
    $this->animal = $animal;
  }
}

$animal1 = new Animal();
$animal1->name = "Fluffy";
$animal1->age = 5;

$dog1 = new Dog($animal1);

$dog2 = clone $dog1;

$dog2->animal->name = "Fido";

echo $dog1->animal->name; // outputs "Fido"
echo $dog2->animal->name; // outputs "Fido"

In this example, a class called “Animal” is defined with two public properties called “name” and “age”. Another class called “Dog” is defined with a public property called “animal” that references an “Animal” object. An object of the “Animal” class called “$animal1” is created and its properties are set. An object of the “Dog” class called “$dog1” is created and its “animal” property is set to “$animal1”. Another object of the “Dog” class called “$dog2” is created by cloning “$dog1”. The “$dog2” object has its own copy of the “$dog1” object’s properties, but the “animal” property of both “$dog1” and “$dog2” objects refer to the same “$animal1” object. This means that if you change the “name” property of the “$animal1” object through either “$dog1” or “$dog2”, the change will be reflected in both objects.

In conclusion, the “clone” keyword in PHP is used to create a copy of an object. When an object is cloned, a new object is created with a copy of the original object’s properties and methods. The “__clone” magic method can be used to customise the cloning process for an object, but it’s important to note that any object references in the original object will not be copied to the new object. Understanding the “clone” keyword is useful for creating copies of objects in your PHP code.