Exciting New Features in PHP 8.1

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

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:

The digital services landscape is constantly shifting, with new trends emerging every year that redefine how businesses must approach their online strategies. This year’s Cloudflare 2024 Year in Review reveals...

Data is the cornerstone of effective decision-making. From tracking website traffic to analyzing customer behavior and measuring the success of campaigns, data drives nearly every aspect of a marketer’s job....

In the dynamic and competitive world of digital marketing, success is rooted in a deep understanding of your audience’s intent. Why are they searching? What information or solution are they...

Imagine a company dedicating months to crafting a perfect marketing strategy: detailed research, precise audience segmentation, a compelling message, and innovative ideas. The strategy looks flawless on paper, but when...

Ready for more?

Subscribe to our newsletter to stay up to date on the latest web design trends, digital marketing approaches, ecommerce technologies, and industry-specific digital solutions.

Name