0
0
PHPprogramming~10 mins

__clone for object copying in PHP - Interactive Code Practice

Choose your learning style9 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'.