What’s New In PHP 8.2?

PHP 8.2 was released in December 2022 as the latest minor version in the PHP 8.x release cycle. It adds new features that build upon the capabilities of previous versions, further simplifying the development experience. In this article, we’ll tour each of the major changes and show how they’ll make your code easier to maintain. Type System Improvements PHP has been gradually evolving its type system towards a more strongly typed model over the past several releases. 8.2 includes two enhancements that allow types to be even more expressive. Disjunctive Normal Form: Combined Unions and Intersections Union and intersection types can now be combined anywhere a type is accepted, such as function parameters and return values. The complete type definition must be written using boolean disjunctive normal form notation (DNF). This means intersection types have to be wrapped in parentheses to be valid. The following function allows you to pass either an array, or an object that implements both the Countable and Iterator interfaces: function foo((Countable&Iterator)|array $values) : void { // … } This facilitates more powerful type definitions, similar to the example function shown above. Prior to PHP 8.2, you’d have to write two separate functions to achieve the same effect, one for each of the types in the union. Standalone Types for Null and Booleans The values true, false, and null are now accepted as standalone types. They can be used in any type definition to indicate that the specific value will be returned: function alwaysReturnsTrue() : true… Click below to read the full story from How To Geek
Read More