Copy or Share · Level 4 of 8

clone is shallow

Clone a user that holds an address object, then change the clone's address.

<?phpclass Address{    public function __construct(public string $city) {}}class User{    public function __construct(public string $name, public Address $address) {}}$u1 = new User('Ann', new Address('Oslo'));$u2 = clone $u1;$u2->name = 'Bob';$u2->address->city = 'Rome';echo $u1->name, ' ', $u1->address->city, PHP_EOL;echo $u2->name, ' ', $u2->address->city, PHP_EOL;

1. Predict the output

Tap the lines in the order you think they print.

  1. 1…
  2. 2…