What’s New In PHP 8.1?

Configurare noua (How To)

Situatie

PHP 8.1 was released in November 2021 as the latest minor version of the PHP language. It adds several new language features alongside some smaller improvements and performance enhancements. There are a few breaking changes to be aware of but most upgrades from PHP 8.0 should be straightforward.

Solutie

New Features

This year’s PHP update adds several new features to enhance developer productivity. Here’s what you’ll be able to use after you upgrade.

Enums

Enum types are finally part of the PHP language. They let you specify that a value must be one of a set of predefined constants. Compared with regular constants, enums give you built-in validation when used as method parameters and return values. They may also have backing values and methods that attach extra behavior.

Readonly Properties

The new readonly keyword marks a class property as immutable. Readonly properties can only be written to once. Trying to change their value after initialization will throw an error. This simplifies the creation of simple immutable value objects. Previously you needed to add repetitive methods to a class if you wanted to expose property values without allowing modification. Now you can make the properties public without risking unintentional mutations.

Intersection Types

PHP’s type system now understands intersections. These let you specify that a value must implement multiple interfaces.

This is useful in cases where code needs to call instance methods that are defined by two different interfaces. Previously you needed to create a new interface extending both of the desired ones. This wasn’t always practical in cases where you couldn’t modify the target class to implement your new interface.

Intersections are defined by combining two or more types with an ampersand haracter. They add more flexibility to the type system after the introduction of union types in PHP 8.0. One caveat is you can’t currently use intersection and union types together – Countable&Stringable|CustomType is off limits in this release.

Fibers

Fibers are mechanism to facilitate concurrent execution. They make it easier to implement resumable code blocks that can be suspended from anywhere in the stack.

The Fibers API is relatively low-level. It’s not expected that end user developers will interact with it on a regular basis. Instead, Fibers will be integrated into libraries that offer async APIs and event loop implementations. They’re a control flow system that enables simplified higher-level abstractions.

Fibers offer a path to async function calls that look like regular synchronous operations. You can cut out the boilerplate associated with promises and callbacks. Fibers handle suspending and resuming the code at the appropriate points, offering one API for blocking and non-blocking implementations of operations.

Conclusion

PHP 8.1 adds many new features that make the development experience easier and more streamlined. Enums have long been a missing piece of the type system while readonly properties and new in initializers will make it quicker to write new classes.

Fibers help to make async PHP more approachable while first-class callables facilitate streamlined function references when practicing functional programming techniques. All these changes further mature PHP as a flexible language that offers strong safety guarantees for your code while still being simple to work with.

Tip solutie

Permanent

Voteaza

(4 din 8 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?