Features that make PHP go forward
Tags: php, features, modernisation
Published: 2024-01-19 21:14:20

#Features that make PHP go forward 

Open source projects often support older PHP versions, and sometimes, really much older. At some point, they take the decision to move on. With the drop of older versions, (relatively) new features can be used to modernize the sources. These features are the ones that make PHP go forward: they are wanted and awaited.

To detect which PHP features are important enough to trigger the abandonning of arcane PHP versions, we went a wide spanning audit and linted 51 modern PHP projects with PHP 8.0 and 7.4. Then, we checked what syntax error does the code yield with old PHP versions. By using these features, the backward incompatibility with now gone. 

## General compatibility with PHP 7.4 and 8.0

Out of the 51 projects, 18 projects generate no linting error with PHP 7.4; 20 projects generate no linting error with PHP 8.0. This gives 35% of PHP projects are still ensuring backward compabitibility with PHP 7.4; and a little less than 40% are ensuring PHP 8.0 backward compatibility. 

The other projects are all displaying some incompatibility with these versions. It ranges from 1 file to 695 files, or up to 14% of the code base, incompatible with PHP 8.0. 

## Most frequent adopted features for PHP 8.0

Here are the most frequent features that are making the code backward incompatible. The percentage is the number of projects that are displaying a related error, divided by the number of projects that are backward incompatible. For example, an enumeration ``enum e {}`` yield `` syntax error, unexpected identifier "e" `` in PHP 8.0, and not in PHP 8.1. 


+ [enum](https://www.php.net/manual/en/language.types.enumerations.php): 45%
+ [readonly property](https://www.php.net/manual/en/language.oop5.properties.php): 35%
+ [first class callable](https://www.php.net/manual/en/functions.first_class_callable_syntax.php): 22%
+ [intersectional type](https://www.php.net/manual/en/language.types.type-system.php) : 22%
+ [new initializer](https://wiki.php.net/rfc/new_in_initializers) : 19%
+ [final class](https://www.php.net/manual/en/language.oop5.final.php): 3%
+ [constant visibility](https://www.php.net/manual/en/language.oop5.constants.php): 3%

The enumerations (PHP 8.1) are the feature that most often breaks backward incompability in modern code: it happens in 45% of the projets. 

Then, readonly properties (PHP 8.1) while readonly classes are not yet visible. Intersectional types (A&B, PHP 8.1) and first class callable (strlen(...), PHP 8.1) are all close, with about 20% of compatibility breaks. 

Last, but not least, are constant visibility and final classes. Final classes are not appearing. 

## Most frequent adopted features for PHP 7.4

When projects reach PHP 8.0, they start using the following features: 

+ [promoted properties](https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion): 66%
+ [named parameters](https://www.php.net/manual/en/functions.arguments.php): 42%
+ [throw as expression](https://www.php.net/manual/en/language.exceptions.php): 27%
+ [mixed type](https://www.php.net/manual/en/language.types.mixed.php): 24%

Promoted properties is a clear winner for adoption. It is a reason to drop backward compatibility in 2 cases out of 3. 

## Other omitted errors

There are a lot more incompatibilities between PHP middle versions ([PHP 8.1](https://exakat.readthedocs.io/en/latest/Reference/Rulesets/CompatibilityPHP81.html#ruleset-compatibilityphp81), [PHP 8.2](https://exakat.readthedocs.io/en/latest/Reference/Rulesets/CompatibilityPHP82.html#ruleset-compatibilityphp82), [PHP 8.3](https://exakat.readthedocs.io/en/latest/Reference/Rulesets/CompatibilityPHP83.html#ruleset-compatibilityphp83)...) than simply lint. Notably, the linting process stops at the first encountered error, and there may be a lot more errors later. Yet, this audit only counts the first one. 

Also, some incompatibilities are only visible at execution time: they are left out of this analysis. 

## Choosing the features that make PHP go forward 

It is interesting to discover which are the PHP features that are awaited, in projects that prevents themselves from using them to preserve PHP backward incompatibilities. 

It is a reverse scenario, compared to the delivery of a new PHP version. When the project decides to move one, these new features are widely available, and usually well understood by coders. This prevents using features because they are new or fashionable. Only the real ones are adopted.