PHP 8.1 was released on November 25, 2021. It contains several new features to make developing in PHP even nicer.
PHP 8.1 is a major update that PHP.net says “comes with numerous improvements and new features such as:
- Readonly properties
- Pure Intersection Types
- never return type
- First-class Callable Syntax
- “final” modifier for class constants
- New fsync and fdatasync functions
- New array_is_list function
- Explicit Octal numeral notation
- And much much more…”
If you would like to learn more about these features see the PHP 8.1 Announcement Addendum.
One new addition to PHP is readonly properties.
The readonly property can be added to variables in the __construct arguments of a class. Once the class has been initialized, you are unable to change the values of the property. If you do so a PHP exception will be thrown.
The major benefit of the readonly property is that you can be sure that no side effects from other parts of the code will ever modify the properties once they have been set. This works well when using data-transfer objects and you want to be sure that the data at the end of your processing is the same as it was at the beginning.
PHP has almost always been a synchronous language, meaning that code execution always waits until results are returned. There are a few projects that have added asynchronous functionality to PHP, but nothing in the language itself. With PHP 8.1, Fibers are added to handle parallelism with virtual threads (or green threads).
From the RFC:
- Adding support for Fibers to PHP.
- Introducing a new Fiber class and the corresponding reflection class ReflectionFiber.
- Adding exception classes FiberError and FiberExit to represent errors.
- Fibers allow for transparent non-blocking I/O implementations of existing interfaces (PSR-7, Doctrine ORM, etc.). That’s because the placeholder (promise) object is eliminated. Instead, functions can declare the I/O result type instead of a placeholder object that cannot specify a resolution type because PHP does not support generics.
Fibers are not expected to be used by most developers. This is a feature that is targeted at library authors such as ORMs that handle database connections and libraries that handle HTTP connections.
As PHP libraries and frameworks start to take advantage of Fibers, it will be interesting to see what kind of performance benefits we start to see in the PHP developments space.
Other popular coding languages have included enums for quite some time, and now PHP has a native way for declaring enums. Enumerations (enums) are a data type that is created by a PHP developer and consists of a fixed set of possible values. This feature may not be absolutely necessary, but it is a nice feature to have. Using packages like Spatie’s PHP Enum package or similar, it is possible to simulate Enum-like features with other data structures. However, most projects implement Enums slightly differently from one another so there is some difficulty integrating them together. Once more projects upgrade to PHP 8.1, it will be interesting to see native Enum’s being used more often.
New fsync() and fdatasync() Functions
Two new file system functions called fsync() and fdatasync() have been added. The use of these functions are to instruct the Operating System to flush its buffers to physical storage. Basically, these functions can be used if you need to ensure that the file you are writing is 100% saved to physical storage before continuing execution of your code.
First-class callables are a new shorthand way of referencing functions as a variable.
$fn = Closure::fromCallable('strlen');
You can now do:
$fn = strlen(...);
Note the 3 dots is the syntax needed to make a callable function.
The Never Type
The new never type is a return type hint for functions that should never return. This is only to be used in functions that always exit() or throw an exception. Its primary use is for static analysis to define that the intended behavior of the function is to never return.
PHP 8.1 continues to evolve the language and make it better giving us more flexibility and performance improvements.
In the WordPress ecosystem we still have a while to wait until we can fully roll out PHP 8.1. All plugins need to be updated to support the new version, so it will not be usable until all of the plugins that we use on a site can be updated to support the changes. We expect it to be about a year until we can actually start getting the benefits of PHP 8.1. In the meantime, if you’re looking for web development assistance, contact the experts at Hall.