Source: stitcher.io

PHP 8.1: readonly properties

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. Take for example a look at a DTO in PHP 5.6: And compare it to its PHP 8.0's equivalent: That's already quite the difference, though I think there's still one big issue: all those getters.

So, if you can't change readonly properties, and if you can't unset them, how can you create a copy of your DTOs or VOs and change some of its data?

Well, you can copy over objects with changed readonly properties, if you rely on a little bit of reflection magic.

The readonly properties RFCWhat's new in PHP 8.1Evolution of objects in PHP over the yearsCloning readonly properties
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