Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'new' instead of 'clone' to copy an object.
Using 'copy' which is not a PHP keyword.
✗ Incorrect
In PHP, the 'clone' keyword is used to create a copy of an object.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Resetting the wrong property like 'name'.
Trying to call 'clone' or 'copy' inside __clone method.
✗ Incorrect
Inside __clone, you can reset properties like 'id' to null to indicate a new object.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the nested object directly without cloning.
Using 'new' without parameters.
✗ Incorrect
To clone nested objects, you must clone them explicitly inside __clone.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Cloning the wrong property like 'model'.
Using 'new' instead of 'clone' for the nested object.
✗ Incorrect
Inside __clone, clone the nested 'engine' object to avoid shared references.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Resetting wrong properties or missing to update timestamp.
Using undefined properties like 'name'.
✗ Incorrect
Reset 'id' to null, update 'timestamp' to current time, and modify 'content' to uppercase in __clone.