PHP 8.1 is due to be released in November 2021, we thought we'd round up the notable new features that are being added to the language.
Fibers
Fibers are essentially threads that allow asyncronous execution of PHP code. This has been possible with PHP for a while but Fibers will offer a language level option for those needing it, existing async libraries will no doubt be re-written to take advantage of it. If you've ever used Promises in Javascript Fibers are a bit like that.
Enums
A lot of you have probably been writing your own version of enums for a while, so it's great to have a language level option for this, it'll save a lot of boilerplate code. Essentially an enum is a way to express a value as a type for which it will always be one of the allowed values, you've maybe used them in other languages or as a MySQL data type.
PHP's implementation of enums allow you to assign string or int values so that they can be serialised and/or stored in a db, this is the general use case where they're part of a domain model that's stored in the database.
A good example of an enum is a status value that represents the current state of something like an article.
enum Status: string
{
case: DRAFT = 'draft';
case: PUBLISHED = 'published';
case: ARCHIVED = 'archived';
}
Enums will be able to contain methods, implement interfaces and also had several built in methods that make working with them nice and easy. This is a great addition to PHP.
Array unpacking with string keys
Array unpacking is really useful, it's been part of PHP for a while. Everyone has probably run into the 'Cannot unpack array with string keys' error, this happens as php will only unpack an array with integer keys, the reason for this is down to how unpacked arrays should interact with other array elements when unpacked into arrays. This has been resolved in 8.1, it will be handled in the same way as array_merge, I'm not sure why this wasn't assumed as the best way previously, seems obvious now it's been added!
Readonly properties
Read only properties is another nice addition to the language. Class properties marked as readonly will only be writable once the object is being instantiated. There are a few restrictions when using these but they're logical and I think these are a welcome addition giving a bit more type safety to objects that need it.
New never type
The new never function return type allows functions to declare that they will never return and will infact stop the program execution. This is a relatively limited use case but still will allow more clarity around the intention of certain methods.
Final class constants
The final keyword can now be added to class constants which will stop them being overridden when the class is inherited, this is very useful for some libraries to stop essential values being changed which could affect the way the code behaves in unexpected ways.
There are lots of other smaller changes and deprecations but once again this looks like another good step forward for PHP. We're not fully utilising PHP 8's new features in all of our projects yet so are still getting used to the new features but we're planning to get everything up on to PHP 8 over the next 12 months.