PHP 8.2 introduces read-only classes

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.

Copyright © 2022 IDG Communications, Inc.

Related Posts

The Internet: A Web of Infinite Possibilities

Introduction & Background The Internet has transformed from a niche military research project into the backbone of modern civilization. Born in the late 20th century as a tool for scientists…

The Future of Internet Technology: How AI is Shaping the Digital Revolution

Introduction & Background The internet has transformed from a simple network of computers into a global platform that powers economies, connects societies, and redefines human interaction. Today, artificial intelligence (AI)…

Leave a Reply

You Missed

Future Tech Frontiers: Where AI, Quantum Computing, and Beyond Are Shaping Tomorrow

Future Tech Frontiers: Where AI, Quantum Computing, and Beyond Are Shaping Tomorrow

The Internet: A Web of Infinite Possibilities

The Internet: A Web of Infinite Possibilities

AI-Powered Life: How Tomorrow’s Tech is Reshaping Our World Today

AI-Powered Life: How Tomorrow's Tech is Reshaping Our World Today

The Quantum Leap: How Computers Are Reshaping Tomorrow

The Quantum Leap: How Computers Are Reshaping Tomorrow

The Art of Crypto Trading: Mastering the Digital Gold Rush

The Art of Crypto Trading: Mastering the Digital Gold Rush

Revolutionizing Efficiency: Cutting-Edge Software Solutions for a Smarter Future

Revolutionizing Efficiency: Cutting-Edge Software Solutions for a Smarter Future