Return of 'clone $this' vs return of '$this'?

Yii3 widgets\html are using different kind of returning, like: return clone $this;

in Label.php:

 public function encode(bool $value): self
 {
     $new = clone $this;
     $new->encode = $value;
     return $new;
 }

I have not seen this kind of returning in any part of Yii2. In Yii2 it would be rather:

 public function encode(bool $encode): self
 {
     $this->encode = $encode;
     return $this;
 }

What is the idea behind cloning? I tried to search all over Yii3 forum and Stack Overflow, but no clues. What is benefit? Performance? Memory?

Is this kind of coding style/behaviour defined/suggested in Yii3 docs, where can be more interesting clues to be found?

The idea is immutability: Immutable objects

6 Likes