Recently released, the PHP 8.1 language update incorporates enumeration and read-only capabilities. It also benefits from improvements in concurrency and performance.
Announced as a major update to the popular scripting language for web development, PHP 8.1 adds many capabilities, from enums or enumerations and read-only properties readonly, down to first-class callable syntax. Enums, or enumerations, allow developers to define a custom type, limited to a discrete number of possible values. This feature can be useful when defining a domain model by “making invalid states unrepresentable”, as the PHP documentation explains. In the PHP language, enum instances are valid objects usable anywhere an object can be used, including type checks.
Other features and improvements brought by PHP 8.1 include:
– “Fibers” provide primitives to implement light concurrency.
– With the first-class callable syntax, closures for callables can be created using the syntax myfunc(…), which is identical to Closure::fromCallable(‘myFunc’).
– The property modifier readonly prevents modification of a property after initialization.
– Performance has been improved thanks to a JIT (Just in Time) compiler for ARM64, to opaque also called “Inheritance cache”, a fast resolution of class names and improvements to standard library filesystem iterators.
– With initializers (Initializer), it is now possible to use objects as default parameter values, static variables and global constants, in addition to their possible use in attribute arguments. This effectively allows the use of nested attributes.
– Intersection types can be used when a value must satisfy several type constraints simultaneously.
– Developers can declare final class constants, so that they cannot be overridden by child classes.
– It is now possible to write octal numbers with the explicit prefix 0o.
– A return type never indicates that the function does not return a value.
– Arrays can be unpacked with keys string.
– New classes, functions and interfaces have been introduced, including an attribute for #[ReturnTypeWillChange].
– The transition from null to internal parameters of non-variable functions is deprecated.
PHP 8.1 came almost a year after the release of PHP 8.0, which offered features like 2.0 union types and JIT compilation. Also new to note is the formation of the PHP Foundation, a non-profit organization dedicated to ensuring the longevity and prosperity of PHP. The foundation’s members include JetBrains and Zend. Participants will make donations to cover the salaries of core PHP developers.
PHP 8.1 source code and Windows binaries can be downloaded from php.net.