Bird
Raised Fist0
PHPprogramming~10 mins

__clone for object copying in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to clone the object.

PHP
<?php
class Person {
    public $name;
}

$original = new Person();
$original->name = "Alice";
$copy = [1] $original;
echo $copy->name;
?>
Drag options to blanks, or click blank then click option'
Aclone
Bcopy
Cnew
Dduplicate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'new' instead of 'clone' to copy an object.
Using 'copy' which is not a PHP keyword.
2fill in blank
medium

Complete the __clone method to reset the id property when cloning.

PHP
<?php
class User {
    public $id;
    public $name;

    public function __clone() {
        $this->[1] = null;
    }
}

$user1 = new User();
$user1->id = 1;
$user1->name = "Bob";
$user2 = clone $user1;
echo $user2->id;
?>
Drag options to blanks, or click blank then click option'
Aid
Bname
Cclone
Dcopy
Attempts:
3 left
💡 Hint
Common Mistakes
Resetting the wrong property like 'name'.
Trying to call 'clone' or 'copy' inside __clone method.
3fill in blank
hard

Fix the error in cloning an object with a nested object property.

PHP
<?php
class Address {
    public $city;
}

class Employee {
    public $name;
    public $address;

    public function __clone() {
        $this->address = [1] $this->address;
    }
}

$addr = new Address();
$addr->city = "Paris";
$emp1 = new Employee();
$emp1->name = "Eve";
$emp1->address = $addr;
$emp2 = clone $emp1;
$emp2->address->city = "London";
echo $emp1->address->city;
?>
Drag options to blanks, or click blank then click option'
Acopy
Bnew
Cclone
Dduplicate
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the nested object directly without cloning.
Using 'new' without parameters.
4fill in blank
hard

Fill both blanks to create a deep clone of an object with nested objects.

PHP
<?php
class Engine {
    public $power;
}

class Car {
    public $model;
    public $engine;

    public function __clone() {
        $this->[1] = [2] $this->engine;
    }
}

$engine1 = new Engine();
$engine1->power = 200;
$car1 = new Car();
$car1->model = "Sedan";
$car1->engine = $engine1;
$car2 = clone $car1;
$car2->engine->power = 250;
echo $car1->engine->power;
?>
Drag options to blanks, or click blank then click option'
Aengine
Bmodel
Cclone
Dnew
Attempts:
3 left
💡 Hint
Common Mistakes
Cloning the wrong property like 'model'.
Using 'new' instead of 'clone' for the nested object.
5fill in blank
hard

Fill all three blanks to clone an object and reset its id and timestamp properties.

PHP
<?php
class Document {
    public $id;
    public $content;
    public $timestamp;

    public function __clone() {
        $this->[1] = null;
        $this->[2] = time();
        $this->[3] = strtoupper($this->content);
    }
}

$doc1 = new Document();
$doc1->id = 123;
$doc1->content = "hello";
$doc1->timestamp = 1650000000;
$doc2 = clone $doc1;
echo $doc2->id . ' ' . $doc2->timestamp . ' ' . $doc2->content;
?>
Drag options to blanks, or click blank then click option'
Aid
Btimestamp
Ccontent
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Resetting wrong properties or missing to update timestamp.
Using undefined properties like 'name'.

Practice

(1/5)
1. What does the __clone method do in PHP when you use the clone keyword on an object?
easy
A. It runs automatically to customize the copying process of the object.
B. It deletes the original object after cloning.
C. It converts the object into a string.
D. It prevents the object from being cloned.

Solution

  1. Step 1: Understand the clone keyword behavior

    When you clone an object in PHP, the __clone method runs automatically if it exists.
  2. Step 2: Purpose of __clone method

    The __clone method lets you customize how the object is copied, for example, copying inner objects or resetting properties.
  3. Final Answer:

    It runs automatically to customize the copying process of the object. -> Option A
  4. Quick Check:

    __clone customizes cloning = A [OK]
Hint: Remember: __clone runs automatically on cloning [OK]
Common Mistakes:
  • Thinking __clone deletes the original object
  • Confusing __clone with __toString
  • Believing cloning is prevented by __clone
2. Which of the following is the correct syntax to define a __clone method inside a PHP class?
easy
A. public function clone() {}
B. function __copy() {}
C. public function __clone() {}
D. private function cloneObject() {}

Solution

  1. Step 1: Identify the magic method syntax

    PHP magic methods start with double underscores, so __clone is the correct method name.
  2. Step 2: Confirm method declaration

    The method must be declared as public function __clone() to be called automatically on cloning.
  3. Final Answer:

    public function __clone() {} -> Option C
  4. Quick Check:

    Magic method __clone syntax = B [OK]
Hint: Magic methods start with double underscores __ [OK]
Common Mistakes:
  • Using single underscore or wrong method name
  • Making __clone private or protected
  • Confusing __clone with __copy
3. What will be the output of this PHP code?
class Box {
  public $size;
  public function __clone() {
    $this->size = $this->size * 2;
  }
}

$box1 = new Box();
$box1->size = 5;
$box2 = clone $box1;
echo $box2->size;
medium
A. 5
B. 10
C. 0
D. Error

Solution

  1. Step 1: Understand cloning and __clone method

    When $box1 is cloned to $box2, the __clone method runs and doubles the size property.
  2. Step 2: Calculate the new size value

    Original size is 5, after cloning size becomes 5 * 2 = 10.
  3. Final Answer:

    10 -> Option B
  4. Quick Check:

    size doubled on clone = 10 [OK]
Hint: Check __clone changes after cloning [OK]
Common Mistakes:
  • Assuming size stays same after cloning
  • Forgetting __clone runs automatically
  • Expecting syntax or runtime error
4. Consider this PHP code snippet:
class Item {
  public $value;
  public function __clone() {
    $this->value = $this->value + 1;
  }
}

$item1 = new Item();
$item1->value = 10;
$item2 = $item1;
$item3 = clone $item1;
echo $item2->value . ',' . $item3->value;

What is the output and why?
medium
A. 10,11
B. 11,11
C. 10,10
D. Error

Solution

  1. Step 1: Understand assignment vs cloning

    $item2 = $item1 assigns the same object reference, no cloning happens, so value stays 10.
  2. Step 2: Cloning triggers __clone method

    $item3 = clone $item1 creates a new object and runs __clone, increasing value by 1 to 11.
  3. Final Answer:

    10,11 -> Option A
  4. Quick Check:

    Assignment no change, clone adds 1 = 10,11 [OK]
Hint: Assignment copies reference; clone calls __clone [OK]
Common Mistakes:
  • Thinking assignment clones the object
  • Expecting both values to change
  • Confusing output order
5. You have a PHP class Person with a property address which is an object of class Address. You want to clone a Person object so that the address object is also cloned (deep copy), not just referenced. How should you implement the __clone method in Person?
hard
A. No need to implement __clone; cloning copies all properties deeply by default.
B. public function __clone() { $this->address = $this->address; }
C. public function __clone() { unset($this->address); }
D. public function __clone() { $this->address = clone $this->address; }

Solution

  1. Step 1: Understand shallow vs deep copy

    By default, cloning copies object properties shallowly, so inner objects remain shared references.
  2. Step 2: Implement deep copy in __clone

    To clone the inner address object, you must explicitly clone it inside __clone method.
  3. Final Answer:

    public function __clone() { $this->address = clone $this->address; } -> Option D
  4. Quick Check:

    Deep copy inner objects by cloning them in __clone [OK]
Hint: Clone inner objects inside __clone for deep copy [OK]
Common Mistakes:
  • Assuming clone copies inner objects automatically
  • Assigning inner object without cloning
  • Removing inner object instead of cloning
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard