Category: PHP, PHP

Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. In PHP 8.1, readonly properties aren't allowed to be overridden as soon as they are initialized.

It's very likely PHP will add some kind of mechanism to clone objects and override readonly properties in the future, but with the feature freeze for PHP 8.1 coming up, we can be certain this won't be included for now.

First you download the package using composer, and next use the Spatie\Cloneable\Cloneable trait in all classes you want to be cloneable: Now our Post objects will have a with method that you can use to clone and override properties with: There are of course a few caveats: I imagine this package being useful for simple data-transfer and value objects; which are exactly the types of objects that readonly properties were designed for to start with.

spatie/php-cloneableWhat's new in PHP 8.1The readonly properties RFCEnums in PHP 8.1What's new in PHP 8
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