Paul Krill
Editor at Large

PHP 8.2 introduces read-only classes

news
Dec 13, 20222 mins
PHPProgramming LanguagesWeb Development

Major update to the popular scripting language also features performance and syntax improvements and new stand-alone types.

Web dev

A major update to the stalwart scripting language for web development, PHP 8.2 has arrived with performance, syntax, and type safety improvements along with new capabilities such as read-only classes and stand-alone types.

PHP 8.2 was published December 8 and can be accessed from php.net.

Support for read-only classes in PHP 8.2 means that a class marked with the readonly modifier will mark all instance properties of the class as read-only and prevent the creation of dynamic properties. Marking readonly classes with the AllowDynamicProperties attribute triggers a compile error.

The addition of null, false, and true as stand-alone types in PHP 8.2 is described as a โ€œprogramming enhancement.โ€ The null type corresponds to PHPโ€™s unit type (the type that holds a single value) while false and true are literal types of type bool. These changes promote type system completeness and serve a number of edge cases.

In other improvements in PHP 8.2:

  • Disjunctive normal form (DNF) types enable the combination of union and intersection types, following a strict rule that says when combining union and intersection types, intersection types must be grouped with brackets.
  • A โ€œrandomโ€ extension provides an object-oriented API to random number generation.
  • The creation of dynamic properties has been deprecated, to help avoid mistakes and typos, unless the class opts in by using the AllowDynamicProperties attribute; stdclass allows dynamic properties.
  • New classes, interfaces, and functions are featured, such as a msqli_execute_query function and a SensitiveParameter attribute.
  • Constants now can be defined in traits.
  • The ${} string interpolation has been deprecated.

PHP 8.1, which featured new capabilities regarding enums, syntax, and read-only properties, arrived in November 2021. It was followed by several point releases.

Paul Krill

Paul Krill is editor at large at InfoWorld. Paul has been covering computer technology as a news and feature reporter for more than 35 years, including 30 years at InfoWorld. He has specialized in coverage of software development tools and technologies since the 1990s, and he continues to lead InfoWorldโ€™s news coverage of software development platforms including Java and .NET and programming languages including JavaScript, TypeScript, PHP, Python, Ruby, Rust, and Go. Long trusted as a reporter who prioritizes accuracy, integrity, and the best interests of readers, Paul is sought out by technology companies and industry organizations who want to reach InfoWorldโ€™s audience of software developers and other information technology professionals. Paul has won a โ€œBest Technology News Coverageโ€ award from IDG.

More from this author