0
0
PHPprogramming~10 mins

__clone for object copying in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - __clone for object copying
Create original object
Call clone keyword
__clone method runs?
NoShallow copy created
Yes
__clone method customizes copy
New cloned object ready
When you clone an object, PHP makes a copy. If __clone exists, it runs to customize the copy.
Execution Sample
PHP
<?php
class Box {
  public $color;
  public function __clone() {
    $this->color = 'cloned ' . $this->color;
  }
}
$box1 = new Box();
$box1->color = 'red';
$box2 = clone $box1;
echo $box2->color;
?>
This code clones a Box object and changes the color property in the clone using __clone.
Execution Table
StepActionObject StateOutput
1Create $box1box1.color = null
2Set $box1->color = 'red'box1.color = 'red'
3Clone $box1 to $box2box2.color = 'red' (before __clone)
4__clone runs on $box2box2.color = 'cloned red'
5echo $box2->colorbox2.color = 'cloned red'cloned red
6EndNo more actions
💡 Execution stops after echoing the cloned object's modified color property.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
$box1->colornull'red''red''red''red'
$box2->colorundefinedundefined'red''cloned red''cloned red'
Key Moments - 3 Insights
Why does $box2->color change after cloning?
Because the __clone method runs on the new object and modifies the color property as shown in step 4 of the execution_table.
Does cloning change the original object $box1?
No, cloning creates a new object. $box1 remains unchanged as seen in variable_tracker where $box1->color stays 'red'.
What happens if __clone method is not defined?
PHP makes a shallow copy without changes. The cloned object's properties remain the same as the original before any __clone customization.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $box2->color immediately after cloning but before __clone runs?
A'cloned red'
B'red'
Cnull
Dundefined
💡 Hint
Check step 3 in the execution_table where cloning happens before __clone modifies the object.
At which step does the __clone method modify the cloned object?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the execution_table row describing __clone running on $box2.
If __clone was removed, what would be the output at step 5?
A'red'
B'cloned red'
Cnull
DError
💡 Hint
Without __clone, the cloned object keeps the original property values as shown in the explanation of key moments.
Concept Snapshot
__clone keyword creates a copy of an object.
If __clone() method exists, it runs on the new copy.
Use __clone to customize cloned object properties.
Original object stays unchanged.
Syntax: $copy = clone $original;
__clone is called automatically during cloning.
Full Transcript
In PHP, cloning an object creates a new object with the same properties. When you use the clone keyword, PHP copies the original object. If the class has a __clone method, PHP runs it on the new object to let you change properties or do other setup. This does not affect the original object. For example, if you have a Box object with a color property set to 'red', cloning it creates a new Box with color 'red'. Then __clone can change the color to 'cloned red'. The original Box remains 'red'. This helps when you want a similar object but with some differences. The execution table shows each step: creating the original, setting color, cloning, running __clone, and printing the cloned color. The variable tracker shows how $box1 and $box2 colors change. Key moments clarify why the clone changes only the new object and what happens without __clone. The quiz tests understanding of when and how __clone runs and what values change.