Source: stitcher.io

PHP 8.1: new in initializers
Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. Let's take a look at an example; we've all written code like this: In this state machine example, we'd like to construct our class in two ways: with and without an initial state.

In other words: you can use new for default arguments (which are one example of "initializers"): "Initializers" are more than parameter default values though, here's a simple explanation from the RFC: This RFC proposes to allow use of new expressions inside parameter default values, attribute arguments, static variable initializers and global constant initializers

Maybe it should be able to validate array elements, something like this: Before PHP 8.1, you wouldn't be able to write this kind of code, because you weren't allowed to use new in attributes, due to the way they are evaluated, but now you can!

If you want to stay up to date about what's happening on this blog, you can follow me on Twitter or subscribe to my newsletter: EmailSubscribeThe Road to PHP 8.1The new in initializers RFCReadonly properties in PHP 8.1What'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