<?php class House { /** * @var string */ private $size; public function __construct(string $size) { $this->size = $size; } public function getSize() : string { return $this->size; } public function withSize(string $newSize) : House { $clone = clone $this; $clone->size = $newSize; //note that $size field had to be changed to public return $clone; } } $house = new House('small'); $bigHouse = $house->withSize('big'); echo $house->getSize();
You have javascript disabled. You will not be able to edit any code.