Unlock the Power of Modern PHP Features

As a PHP developer, you want to take advantage of the latest language features, but sometimes, compatibility issues with older versions hold you back. The good news is that you can use a transpiler to convert your modern code into a format compatible with earlier PHP versions. This way, you can enjoy the best of both worlds: write code using the latest features and deploy it on older versions.

Why Transpile to PHP 7.1?

I recently decided to transpile my WordPress plugin to PHP 7.1, even though WordPress still supports PHP 5.6. The reason is that Rector, a powerful reconstructor tool, has limited support for downgrading code to PHP 5.6. Additionally, many third-party dependencies require PHP 7.1, making it a more practical target.

What Features Can Be Transpiled?

Rector currently supports downgrading various PHP 7.4 features, including typed properties, arrow functions, and the null coalescing assignment operator. You can even use some PHP 8.0 features, like union types, in your code!

Transpiling a WordPress Plugin

To demonstrate the transpilation process, I’ll use my own GraphQL API for WordPress plugin. The plugin’s code uses features from PHP 7.4, 7.3, and 7.2, which need to be converted to PHP 7.1. Rector takes care of this conversion, ensuring that the plugin remains compatible with older PHP versions.

Dealing with Third-Party Dependencies

When transpiling third-party dependencies, you need to check if they require PHP 7.2 or above. If so, you’ll need to inspect their source code to determine if the PHP 7.2 code can be transpiled. In my case, most dependencies only used PHP 7.1 code, but one required PHP 7.2.5. Fortunately, the problematic code was not used in my plugin, so I didn’t need to transpile it.

Setting Up Rector

To configure Rector, you need to define the sets of rules to apply to your code. These rules downgrade the code from higher to lower PHP versions. You can even uncomment a line to enable support for PHP 8.0 features when they’re released!

Loading WordPress and Running Rector

To transpile the code, you need to provide the path to WordPress’s source files. Then, run Rector with the --dry-run flag to see the transformations without modifying the original code.

Testing the Transpiled Code

Once you’ve transpiled the code, you need to test it to ensure it works correctly. I use PHPStan to analyze the transpiled code and verify that it’s compatible with PHP 7.1.

Generating the Asset for Production

To generate the asset for production, I use a GitHub action that transpiles the code, modifies the Requires PHP header, and creates a .zip file containing the plugin.

Conclusion

Transpiling your PHP code gives you the freedom to use modern language features while maintaining compatibility with older PHP versions. With Rector and GitHub Actions, you can easily generate assets for production that work on a wide range of PHP versions. So, go ahead and unlock the power of modern PHP features!

Leave a Reply

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