PHP 8.1, billed as a major update to the popular scripting language for web development, has been released with capabilities ranging from enums and read-only properties to first-class callable syntax.
PHP 8.1 was released on November 25. Source code and Windows binaries can be downloaded from php.net.
Enums, or enumerations, allow developers to define a custom type that is limited to a discrete number of possible values. This can be helpful when defining a domain model by “making invalid states unrepresentable,” according to PHP documentation. In PHP, enum cases are valid objects that can be used anywhere an object may be used, including type checks.
Other new features and improvements in PHP 8.1 include Fibers which provide primitives for implementing lightweight concurrency. Meanwhile, with first-class callable syntax, closures for callables can be created using the syntax
myfunc(…), which is identical to
<a href="https://wiki.php.net/rfc/readonly_properties_v2" rel="nofollow">readonly</a>property modifier prevents modification of a property after initialisation while performance has been improved with a JIT (just in time) compiler back end for ARM64, inheritance cache, fast-class name resolution, and improvements to the standard library file system iterators.
With initialisers, objects now can be used as default parameter values, static variables, and global constants, along with being used in attribute arguments. This effectively makes it possible to use nested attributes.
In addition, intersection types can be used when a value needs to satisfy multiple type constraints simultaneously, developers can declare final class constants, so they cannot be overridden in child classes and it is now possible to write octal numbers with the explicit
Delving deeper, an
ever return type indicates the function does not return, arrays can be unpacked with string keys while new classes, functions, and interfaces have been introduced, including an attribute for
#[ReturnTypeWillChange]. This is in addition to passing null to non-variable internal function parameters becoming deprecated.
PHP 8.1 follows by nearly a year the release of PHP 8.0, which offered features such as union types and just-in-time compilation. Also new to PHP this month was the formation of the PHP Foundation, a non-profit organisation intended to ensure the long life and prosperity of PHP, with members such as JetBrains and Zend. Participants will donate to cover salaries for PHP core developers.