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

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