String-keyed array unpack
A simple operation, which wasn’t allowed until PHP 8.1. A code snippet worth a thousand words, here is what you’ll be able to do now:
This seems super simple, but you were only allowed to this with arrays with integer keys. As soon as you tried it with string-keyed arrays, you would get a fatal error.
Note that this new feature produce the exact same result as
array_merge, which you can use with string-keyed array on any PHP version where it is available.
Internationalization of dates and format localization has always been a bit of a pain in PHP. This time is nearly over, and PHP 8.1 will be shipped with the new IntlDatePatternGenerator class.
To be short, the purpose of this class is to guess which format will suit the most to a given locale. To illustrate this, I’ll take the example given in the RFC:
As you can see, we’re going to have have way less headache dealing with date formats and localization. Give
getBestPattern your locale and the “skeleton” you want it to format. Then by calling IntlDateFormatter, you’ll get your formatted date for your locale. I love it.
I already talked about enumerations in an article (Why PHP finally makes you a (more) rigorous developer), but in short, an enumeration allows you to define a set of constants, where the value of these doesn’t matter. What’s important is that these constants are strictly typed. If an attribute or a method argument requires a value from your enumeration, you can’t pass anything else. Here is a code snippet that shows you this behavior:
Everywhere you used many constants and you didn’t care about their values, you can use enumerations. If you care about values, PHP will introduce backed enumeration at the same time, which will allow you to define values for your constant (and keep the pros of strict typing!). This way, you are sure that whoever uses/maintains your code, values are restricted to a certain set.
Pure intersection type
This RFC is still in vote phase, but in really good way to be implemented. Thanks to pure intersection typing, you’ll be able to tell PHP you want your variable to implement two or more interfaces, explicitly.
The motivation described by the RFC is that while this type of check is already possible in PHP, it implies an intermediary interface to do so. Doing it like this is way more convenient and readable.
Fibers are blocks of code that can be started, paused, resumed, terminated by the main code. Also, it is important to note that fibers hold their own state and variables.
Fibers are designed to bring concurrency execution to PHP code. Although it looks like multithreading and parallelism, it is very important to note that fibers are not executed in parallel of the main thread.
What happens is PHP will times to times execute the code of the fiber instead of the main block, before going back to the main one. You have no guarantee that your fiber will be executed at an exact point of time.
A simple usage example would be a big count of elements. Your main code block could count these elements, while your fiber regularly echoes the progression. At some points, PHP will switch from the main code block to your fiber code block to execute a method that fetch the number of elements currently counted and print it to the screen, before returning to the main code block that counts elements.
“Never” return type
The never return type is like nothing else you know about method return types. This keyword indicates that the method will never return anything, explicitly or implicitly. In other words: your method must never reach its final brace.
How is it possible in practice? Well, your method must simply throw an exception, call
exit for sure. Also,
never can not be used in union types, such as
Here is an example of the use of
There are still a lot of RFCs being voted on and discussed right now, and everything is super exciting. But this is already a taste of the future of PHP, which looks bright!