Source: matthiasnoback.nl

Effective immutability with PHPStan
This article is about a topic that didn't make the cut for my latest book, https://leanpub.com/recipes-for-decoupling. I don't know where I first heard it, but PHP's DateTimeImmutable is not immutable: getTimestamp().

But an even bigger surprise might be that if a constructor is public, it's just another method that you can call.

This is exactly the point I want to make in this article: DateTimeImmutable is already effectively immutable because nobody mutates its state.

The downside is that we have to do more work on the classes to make their instances truly immutable.
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