Herdman look back

PHP 8.2 features one year later

Closing on one year after PHP 8.2 publication, and just before the publication of PHP 8.3, it is time to take a look at the PHP 8.2 features, one year later and how they are adopted in current code.

We’ll take the list of ‘PHP Core’ features from the New Features and look for them in a large corpus of Open Source Projects.

See also PHP 8.1 features, one year later.

Project corpus

In this article, we’ll use the Exakat corpus, with 3000 PHP open source projects. Out of those, 1511 were updated since last year, (56%). Those projects had a chance to update their code base to be compatible with PHP 8.2, and so, adopt those features. This is a good base for assessing the adoption of features.

Feature adoption and backward compatibility

The PHP 8.2 new features are all backward incompatible : adopting them means that the code cannot be run anymore in older versions, including PHP 8.1. In the case of Open Source projects, with long support time, this means that the new features adoption may be rather slow. It should be a lot slower than in closed code, which doesn’t have to deal with such compatibility.

In this study, the absolute numbers of adoption have biaised values, since all the repositories are open source.

On the other hand, the relative adoption of the features shows their popularity. This is the most interesting part of this review.

That said, let’s dive.

New attribute SensitiveParameter

SensitiveParameter is a new PHP attribute, that signals a piece of data that should never be displayed: this is the case for passwords, SSH keys, unique tokens, etc. When assigned to a parameter, this attribute prevents the display of the content of the parameter in a stack trace.

There is also a rarely seen class called SensitiveParameterValue, which acts the same, but for anything else than a parameter.

SensitiveParameterValue has been spotted in 7 projects, out of 1156. It is used 346 times. This shows that it is a niche feature, and it is critical to anyone who needs to use it.

See it in action at Symfony, kirby or osticket.

Enumerations properties in constant expressions

Enumerations’s cases properties can be used in static constant expressions.

We could not find any usage of that feature.

Type System Improvements: DNF

DNF, also known as Disjunctive Normal Form, is an extension of the PHP type system, where intersectional and union types can be mixed in a single type.

DNF has been found in 127 projects, with up to 13 cases of usage. This is a slow start, with a good level of projects, and rare opportunities.

Some of the highest usage of that feature are at phpspec, codeception, phpunit. So, it seems quite useful to use with testing framworks, so as to provide an extensive set of types, and also include mock classes.

Constants in Traits

Constants in traits have been spotted in 3 projects. In constrast, traits are quite widespread, with 33% of adoption across the repositories.

Readonly Classes

Readonly classes are classes where all the properties are readonly. This feature was found in 70 projects, in particular resonnance, lemmyApi and elephpox.

true, false, null

true, false and null are now full standalone types. Null was introduced as an alternative to ? and is written as a union type.

False was needed to type PHP native functions, which often return false or an actual result. True was added to make this set complete. True and false are a special type, called a value type where the type is also the value.

Those types have been found in 662 projects, including tuleap, gatographql, pop and symfony. The overwehlming majority of the cases are with null. The remainder are cases of type propagation: some native PHP function returns false, and it is directly fed as a return value.

Final round up: PHP 8.2 features, one year later

The top 3 PHP 8.2 features are :

  • null, false and true as standalone type : 57%
  • DNF: 11%
  • Readonly classes: 6%
  • Sensitive parameter : 0.6%
  • Constants in traits: 0%
  • Enumerations in constants expression : 0%

New PHP 8.2 features have different fates: sometimes, they are massively adopted, others take time to find they audience. The Sensitive parameter and DNF features also show that features may be very useful to a niche in the PHP community.

Recent statistics place PHP 8.2 adoption around 27% of PHP installations. With the growing number of PHP 8.2 usage, we’ll also see a growing adoption of the features.

Exakat include rules to report usage of all those PHP features and more. They can be used to track backward incompatibility, features usage and simply PHP version adoptions.