PHP 8.3 adds the possibility of overwriting readonly property values while cloning an object. The so called clone with syntax (which doesn't exist): readonly class Post { public function __construct( public string $title, public string $author, public DateTime $createdAt, ) {}} $post = new Post( title: 'Hello World', ); $updatedPost = clone $post with { title: 'Another One!', }; Reading the title of the current https://wiki.php.net/rfc/readonly_amendments: "Readonly properties can be reinitialized during cloning" — you might think something like clone with this is now possible. The RFC allows only allows for one specific operation: to overwrite readonly values in the magic __clone method: readonly class Post { public function __construct( public string $title, public string $author, public DateTime $createdAt, ) {} public function __clone(){ $this->createdAt = new DateTime(); }}

readonly class Post { public function __clone(){ $this->createdAt = clone $this->createdAt; }}

Thanks to this RFC, we can make real clones, with all their nested properties cloned as well, even when these properties are readonly: $post = new Post(); $otherPost = clone $post; $post->createdAt->add(new DateInterval('P1D')); $otherPost->createdAt === $post->createdAt; #on-a-personal-note On a personal note
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners