Digital Insights Blog > Exciting New Features in PHP 8.1
Exciting New Features in PHP 8.1
- 3 min read
Highlights
- PHP 8.1, launched in November 2021, introduces several novel features and enhancements, with performance boosts up to 8%.
- Introduced constructs include Enums for typed-value collections, Fibers for asynchronous code execution, and Intersection Types for multi-interface value assignments.
- First-Class Callables enable more simplified functional programming, letting functions be assigned to variables directly.
- PHP 8.1 also enhances code self-documentation with the `never` return type and explicit octal integer notation.
- Added functions include `array_is_list()` for list checking, `fsync()` and `fdatasync()` for file pointer changes synchronization, and support for XChaCha20 and Ristretto255 in Sodium.
The latest version of the PHP language, PHP 8.1, was released in November 2021. It includes a number of new language features as well as some minor upgrades and efficiency improvements. Although there are a few breaking changes to be aware of, the majority of upgrades from PHP 8.0 should be simple.
PHP 8.1 New Features
This PHP release offers a number of new capabilities to help developers work more efficiently. After you upgrade, you’ll be able to enjoy the following features.
Enums (Enumerations)
The PHP language now includes enum types. They allow you to indicate that a value must be one of a predefined set of constants.
If used as method parameters and return values, enums provide built-in validation when compared to regular constants. They may also provide backing values and methods that add additional functionality.
Enums have the advantage of representing a collection of constant values, but more importantly, these values can be typed.
Fibers
PHP had previously only been used to write synchronous code. Fibers, an interesting new feature built right into PHP 8.1, allows you to write asynchronous code.
Fibers are designed to eliminate the distinction between asynchronous and synchronous functions.
Fibers can be used to create full-stack, interruptible PHP functions that can subsequently be used to achieve PHP cooperative multitasking.
Fibers stop the entire call stack, so the function’s immediate caller doesn’t have to change the way it calls the function. Fiber::suspend() can be used to suspend execution at any point in the call stack.
First-Class Callables
In PHP, callables are now considered first-class citizens. This means you can assign functions to variables directly without having to use the old array syntax or create explicit closures.
This improvement will be most useful in the context of functional programming. Direct variable assignment makes passing functions around cleaner and easier.
Intersection Types
The type system in PHP now understands intersections. You can use these to tell a value that it has to implement several interfaces.
This is handy in situations where code needs to call instance methods from two separate interfaces. You used to have to develop a new interface that extended both of the desired ones. When you couldn’t change the target class to implement your new interface, this wasn’t always possible.
You can combine two or more types with an ampersand character to define intersections. After the introduction of union types in PHP 8.0, they give the type system more freedom. One caveat is that you can’t utilize intersection and union types at the same time right now.
Other Features
Aside from the major changes stated above, PHP 8.1 also includes a few handy features that make code more self-documenting. You can use the new never return type to denote functions that will throw an exception or terminate the current script, and you can write octal integers with explicit notation, such as 0o14. This helps in the discovery of dead code in static analysis software.
The standard library now includes a few new functions. array_is_list() checks if an array is a list ([“foo”, “bar”]) or associative ([“foo” => “bar”]), and provides a built-in implementation for a function typically seen in current userland code. File pointer changes can be synchronized back to the underlying storage media using fsync() and fdatasync(). Sodium now has support for the XChaCha20 and Ristretto255 hashing algorithms.
Finally, this update gives the chance of improved performance. A class inheritance cache and other optimizations could result in a free performance boost of up to 8%. This would be most apparent in codebases that are largely object-oriented and have a lot of parent-child interactions. PHP will now cache these URLs rather than rebuilding them on a regular basis.
Conclusion
PHP 8.1 introduces plenty of new capabilities that simplify and streamline the development process. Readonly properties will make it easier to design new classes, and enums have long been a missing piece of the type system.
Fibers make async PHP more approachable, while first-class callables make it easier to use functional programming approaches with simplified function references. All of these features help PHP grow as a flexible language that provides strong security guarantees for your code while being simple to use.
A global team of digerati with offices in Washington, D.C. and Southern California, we provide digital marketing, web design, and creative for brands you know and nonprofits you love.
Follow us to receive the latest digital insights:
- 8 min read
Video is no longer a supporting asset in digital marketing. It is often the first touchpoint, the most persuasive proof point, and the strongest conversion driver in a campaign. Whether...
- 6 min read
Organizations that operate with layered governance, distributed teams, and complex compliance requirements need more than a basic website. They need a digital platform that reflects how their organization actually functions....
- 7 min read
Content marketing services have reached a turning point. Most organizations already invest in blogs, guides, videos, and social content. The problem is not effort. The problem is accountability. Too much...
- 5 min read
A brand discovery workshop is a structured, stakeholder-led session that clarifies a brand’s purpose, audiences, positioning, messaging, and visual direction to align teams and guide strategy, creative, and digital execution....