Downgrade PHP Code: 8.0 to 7.x with Rector

The PHP 8.0 Conundrum: Can We Use It Now?

As PHP 8.0 is set to release by the end of this year, developers are eager to tap into its exciting new features. However, the question remains: can we introduce it into our projects immediately? The answer is not a straightforward one, especially for WordPress users.

The WordPress Dilemma

WordPress, with its massive user base, is particularly affected by this issue. Its upcoming release in December plans to bump up the minimum required PHP version from 5.6 to 7.1. However, due to the fact that nearly 24% of installations still run on either PHP 5.6 or 7.0, the PHP version bump has been temporarily canceled. This means that developers who want to use the latest PHP improvements are facing a significant roadblock.

The Problem with Themes and Plugins

Themes and plugins are not bound by WordPress’s PHP requirements, so they could technically require version 7.1 or higher. However, doing so would limit their potential reach, as only a small percentage of installations currently run on PHP 7.4, and even fewer will immediately adopt PHP 8.0 after its release. This makes it challenging to introduce new features like typed properties or union types into the codebase.

A Glimmer of Hope: Transpiling

So, what’s the solution? One approach is to use a transpiler, a tool that takes source code written in a programming language and produces an equivalent source code in the same or a different language. Babel, a popular toolchain, allows developers to convert ECMAScript 2015+ code into a backwards-compatible version of JavaScript. Similarly, a transpiler for PHP could enable us to write code using PHP 7.4 features and convert it into equivalent code that can run on older versions.

Upgrading the Development Toolchain

Transpiling also offers another benefit: it allows us to access newer versions of libraries used for development. For instance, PHPUnit, a framework for testing, can be upgraded to version 9.x or 8.x if we transpile our code.

Evaluating New Features

Not all new features can be transpiled, however. Features like typed properties and union types introduce new syntax for brand-new features and cannot be directly reproduced in previous PHP versions. However, we can remove them completely and use docblock tags to describe their nature, making the transpiled code still usable.

Rector: A PHP Transpiler

Rector is a reconstructor tool that can transpile PHP code. It executes a sequence of operations: parsing PHP code into an Abstract Syntax Tree (AST), applying rules to execute transformations on selected nodes of the AST, and dumping the new AST back to the file. We can supply Rector with transformation rules to downgrade PHP code from version 8.0 to 7.0.

Configuring Rector

To downgrade code from PHP 7.4 to 7.1, we need to execute sets downgrade-php74 and downgrade-php72. Running the command with --dry-run shows the results in diff format, allowing us to preview the transformations without applying them.

A Compromise

Transpiling is a solution that offers a compromise between developers’ desire to access the latest tools and language features and the need to target a broad user base. With Rector, we can write code containing PHP 8.0 features and deploy it to an environment running a lower version of PHP, all the way down to PHP 7.0. This is a game-changer for developers who want to use the latest PHP improvements without sacrificing compatibility.

Get Started with LogRocket

Want to create better digital experiences? Over 200,000 developers use LogRocket to monitor their applications and identify issues. Get started today and see how LogRocket can help you build better software!

Leave a Reply

Your email address will not be published. Required fields are marked *