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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners