Why it's important to know about PHP version compatibility, where to find information about PHP version updates, and how to test if your plugins and themes support the latest PHP version in two ways.
Why should I test for PHP version compatibility?
![Image[1]-How to test WordPress products for PHP compatibility - Photon Flux | Professional WordPress Repair Service, Global Reach, Fast Response](https://www.361sale.com/wp-content/uploads/2024/03/image-377-1024x585.png)
WordPress is written in the PHP language, which means that it needs to run smoothly on a version of PHP that is supported by your web host. While WordPress will recommend a minimum PHP version requirement, older versions of PHP will gradually stop being updated or even provide security patches.
For example, the recommended minimum PHP version for WordPress right now is 7.4, but that version stopped being updated on 11/28/2022.
The core WordPress code works on PHP 8.0 and PHP 8.1 and also supports PHP 8.2 and PHP 8.3 versions. However, not all plugins are guaranteed to work properly on these newer versions of PHP.
So, if you are a plugin developer, it is very important to regularly check the compatibility of your plugin with different PHP versions.
Where to find information about PHP version changes
In order to find out when and how PHP versions change, it is best to visit the official PHP website at https://www.php.net/.
exist"Supported versions"page, you can find information about which versions are currently supported, the level of support, and which versions have been discontinued.
As of this writing, all PHP 7.x releases are end-of-life, and PHP 8.0 supports only security fixes and actively supports PHP 8.1 as well as PHP 8.2 and 8.3, meaning that bugs and security flaws will be fixed. Please note that PHP 8.0 only supports security fixes until November 2023, around the time PHP 8.3 is released, and then PHP 8.0 will be considered end-of-life.
In the Appendix section of the PHP documentation, you can find a guide to migrating from older PHP versions, which lists the most important changes between the old and new versions. For example.Migrating from PHP 7.4.x to PHP 8.0.xThe guide lists the most important changes between PHP 7.4 and PHP 8.0.
![Image [2] - How to test WordPress products for PHP compatibility - Photon Flux | Professional WordPress Repair Service, Global Reach, Fast Response](https://www.361sale.com/wp-content/uploads/2024/03/image-378-1024x585.png)
Sample plug-ins
<?php/*** Plugin Name: WP Learn Compatibility* Description: Learn to test a plugin for PHP Version Compatibility* Version: 1.0.1** @package wp-learn-compatibility*//*** Posts fetcher class*/class Post_Fetcher {/*** Array posts** @var array*/protected $posts;/*** Fetch the WordPress posts*/public function post_fetcher() {$this->posts = get_posts();}/*** Fetch the posts and return the formatted HTML* @return string* @return string*/public function fetch_posts() {$post_html = '<div class="post">';foreach ( $this->posts as $post ) {if ( property_exists( $post, 'post_title' ) ) {$post_html . = sprintf('<h4><a href="/en/%s/">%s</a></h4>',get_permalink( $post->ID ),$post->post_title);}}$post_html . = '</div>';return $post_html.}}add_shortcode( 'wp_learn_php_compatibility', 'wp_learn_php_compatibility_shortcode_render' );/*** Shortcode callback function for wp_learn_php_compatibility shortcodeshortcode shortcode_render' ); /** * @return string* @return string*/function wp_learn_php_compatibility_shortcode_render() {$post_fetcher = new post_fetcher();$post_html = $post_fetcher->fetch_posts();return $post_html;}<?php /** * Plugin Name: WP Learn Compatibility * Description: Learn to test a plugin for PHP Version Compatibility * Version: 1.0.1 * * @package wp-learn-compatibility */ /** * Posts fetcher class */ class Post_Fetcher { /** * Array posts * * @var array */ protected $posts; /** * Fetch the WordPress posts */ public function post_fetcher() { $this->posts = get_posts(); } /** * Fetch the posts and return the formatted HTML * @return string * @return string */ public function fetch_posts() { $post_html = '<div class="post">'; foreach ( $this->posts as $post ) { if ( property_exists( $post, 'post_title' ) ) { $post_html . = sprintf( '<h4><a href="/en/%s/">%s</a></h4>', get_permalink( $post->ID ), $post->post_title ); } } $post_html . = '</div>'; return $post_html. } } add_shortcode( 'wp_learn_php_compatibility', 'wp_learn_php_compatibility_shortcode_render' ); /** * Shortcode callback function for wp_learn_php_compatibility shortcode shortcode shortcode_render' ); /** * @return string * @return string */ function wp_learn_php_compatibility_shortcode_render() { $post_fetcher = new post_fetcher(); $post_html = $post_fetcher->fetch_posts(); return $post_html; }<?php /** * Plugin Name: WP Learn Compatibility * Description: Learn to test a plugin for PHP Version Compatibility * Version: 1.0.1 * * @package wp-learn-compatibility */ /** * Posts fetcher class */ class Post_Fetcher { /** * Array posts * * @var array */ protected $posts; /** * Fetch the WordPress posts */ public function post_fetcher() { $this->posts = get_posts(); } /** * Fetch the posts and return the formatted HTML * @return string * @return string */ public function fetch_posts() { $post_html = '<div class="post">'; foreach ( $this->posts as $post ) { if ( property_exists( $post, 'post_title' ) ) { $post_html . = sprintf( '<h4><a href="/en/%s/">%s</a></h4>', get_permalink( $post->ID ), $post->post_title ); } } $post_html . = '</div>'; return $post_html. } } add_shortcode( 'wp_learn_php_compatibility', 'wp_learn_php_compatibility_shortcode_render' ); /** * Shortcode callback function for wp_learn_php_compatibility shortcode shortcode shortcode_render' ); /** * @return string * @return string */ function wp_learn_php_compatibility_shortcode_render() { $post_fetcher = new post_fetcher(); $post_html = $post_fetcher->fetch_posts(); return $post_html; }
In this tutorial, we will go through a sample plugin. This plugin creates a special shortcode that, when you use it on a page, displays a list of posts, including the title of each post. A shortcode calledpost_fetcher
class is responsible for getting information about these posts.
When you test this shortcode on a server with PHP version 7.4, you can see that it works properly, displaying the list of posts as we expect.
![Image [3] - How to test WordPress products for PHP compatibility - Photonflux.com | Professional WordPress Repair Service, Global Reach, Fast Response](https://www.361sale.com/wp-content/uploads/2024/03/image-379-1024x585.png)
How to test PHP version compatibility
To check PHP version compatibility, there are several different ways of testing, which may involve using different versions of PHP and some special tools. In this tutorial, we'll introduce two testing methods and explore the advantages and possible shortcomings of each.
Manual Compatibility Testing
The manual testing method requires you to set up the version of PHP you want to test in your WordPress environment and then test your plugin in that environment.
You can set up such an environment in a couple of different ways, the most common way is to use a local development tool that makes it easy to switch between PHP versions, such as Mamp, Laragon, LocalWP, or DevKinsta.
In this tutorial, for example, we will choose PHP version 8.1 for testing.
To quickly confirm which version of PHP your WordPress is using, you can create a file called info.php in your WordPress installation directory and fill it with a specific piece of code.
<?phpphpinfo();<?php phpinfo();<?php phpinfo();
Then, navigate to the info.php file in your browser and you should see the PHP version displayed.
After setting up your test environment, you need to enable WordPress debugging.
To do this, edit thewp-config.php
file and update the lines defining the constantsWP_DEBUG
, set it totrue
define( 'WP_DEBUG', true );define( 'WP_DEBUG', true );define( 'WP_DEBUG', true );
Also, addWP_DEBUG_DISPLAY
constant and set it to false, then add theWP_DEBUG_LOG
constant and set it to true to log errors to the directorydebug.log
in the documentwp-content
The
define( 'WP_DEBUG_DISPLAY', false );define( 'WP_DEBUG_LOG', true ).define( 'WP_DEBUG_DISPLAY', false ); define( 'WP_DEBUG_LOG', true ).define( 'WP_DEBUG_DISPLAY', false ); define( 'WP_DEBUG_LOG', true ).
You can alsoWP_DEBUG_LOG
Set the constants to a custom location by specifying the file path. Example:
define( 'WP_DEBUG_LOG', '/home/ubuntu/wp-local-env/sites/learnpress/logs/debug.' . date( 'Y-m-d' ) . '.log' );define( 'WP_DEBUG_LOG', '/home/ubuntu/wp-local-env/sites/learnpress/logs/debug.' . date( 'Y-m-d' ) . '.log' );define( 'WP_DEBUG_LOG', '/home/ubuntu/wp-local-env/sites/learnpress/logs/debug.' . date( 'Y-m-d' ) . '.log' );
Then, test your plugin by refreshing the page. Please note that the shortcode functionality is broken.
If you look at debug.log, you will see the following error displayed:
[24-Nov-2023 12:59:49 UTC] PHP Warning: foreach() argument must be of type array|object, null given in /home/ubuntu/wp-local-env/sites/learnpress /wp-content/plugins/wp-learn-php-compatibility/wp-learn-php-compatibility.php on line 36[24-Nov-2023 12:59:49 UTC] PHP Warning: foreach() argument must be of type array|object, null given in /home/ubuntu/wp-local-env/sites/learnpress /wp-content/plugins/wp-learn-php-compatibility/wp-learn-php-compatibility.php on line 36[24-Nov-2023 12:59:49 UTC] PHP Warning: foreach() argument must be of type array|object, null given in /home/ubuntu/wp-local-env/sites/learnpress /wp-content/plugins/wp-learn-php-compatibility/wp-learn-php-compatibility.php on line 36
Now, if you go to line 36 of the plugin file, you'll see the following code, and you'll see that it's trying to loop through that$this->posts
attribute, which is null for some reason. the reason may not be obvious, so you may want to dig deeper into theMigrating from PHP 7.4.x to PHP 8.0.xGuide.
In the Backward Incompatible Changes section, you will see the following changes:
Methods with the same name as the class are no longer interpreted as constructors. The __construct() method should be used instead.Methods with the same name as the class are no longer interpreted as constructors. The __construct() method should be used instead.Methods with the same name as the class are no longer interpreted as constructors. The __construct() method should be used instead.
So in this case, our class constructor method needs to be updated.
After the fix, refresh the page and you will see the plugin working as expected again.
While manual testing does work, it can be a tedious process. Fortunately, you can also automate most tests using a tool called PHPUnit. This will allow you to continually protect your code from bugs and PHP compatibility issues, but that's beyond the scope of this tutorial.
![Image [4] - How to test WordPress products for PHP compatibility - Photonflux.com | Professional WordPress Repair Service, Global Reach, Fast Response](https://www.361sale.com/wp-content/uploads/2024/03/image-380-1024x585.png)
Scanning your code with PHPCompatibilityWP
You can also use a number of tools to test PHP compatibility, the most useful being the appropriately namedPHPCompatibility tool, which isPHP_CodeSnifferA set of rules for tools.
PHP_CodeSniffer is a command line tool that can be used to scan code for errors and warnings, while the PHPCompatibility tool is a set of rules that can be used with PHP_CodeSniffer to scan code for PHP version compatibility.
For WordPress developers, there's a new program calledPHPCompatibilityWPwhich is specific to the WordPress project's PHPCompatibility ruleset.
The beauty of PHPCompatibility/PHPCompatibilityWP is that you don't need to configure a different version of PHP to use it. You can use it with an existing version of PHP and it will check your code against the rules of the PHP version you specify.
To install and use PHPCompatibilityWP, you need to install Composer, which is a dependency manager for PHP projects.
In order for Composer to work, you will also need to have PHP installed on your computer so that you can use the PHP CLI binary, which allows you to run PHP scripts in the terminal, not just in your browser.
Installing Composer is beyond the scope of this course, but you can find it in theComposer websiteFind out what's applicable to themacOS/Linuxrespond in singingWindows (computer)Description of the operating system.
You can find theFind out how to install PHP on your system in the "Installation and Configuration" section.
After installing PHP, be sure to add the path to the PHP CLI binary to your computer's operating system path so that you can run PHP commands from anywhere on your computer.
You can check if PHP is installed by running the following command in a terminal:
php -vphp -vphp -v
Similarly, you can check if Composer is installed by running the following command in a terminal:
composer-Vcomposer-Vcomposer-V
After installing Composer, you can initialize the Composer project by running the following command in the plugin directory. If you are already using Composer in a plugin, you can skip this step.
composer initcomposer initcomposer init
This will initialize a new Composer project in your plugins directory. You can accept the default values for most questions, but when it asks you to define dependencies interactively (require) and define your development dependencies interactively (require-dev)? you should answer "No". You can also skip the PSR-4 autoload mapping.
After this is done, you will have acomposer.json
file, which is used by Composer to manage project dependencies.
Next, you need to install the Composer plugin and manage the installation path settings for PHP_CodeSniffer by running the following command from the command. If you already have this plugin installed, you can ignore it.
composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer truecomposer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer truecomposer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
You can then install the Composer installer plug-in and the PHPCompatibilityWP tool by running the following command:
composer require --dev dealerdirect/phpcodesniffer-composer-installer:"^1.0"composer require --dev phpcompatibility/phpcompatibility-wp: "*"composer require --dev dealerdirect/phpcodesniffer-composer-installer:"^1.0" composer require --dev phpcompatibility/phpcompatibility-wp: "*"composer require --dev dealerdirect/phpcodesniffer-composer-installer:"^1.0" composer require --dev phpcompatibility/phpcompatibility-wp: "*"
This will set up and install the required dependencies in your composer.json file.
![Image [5] - How to test WordPress products for PHP compatibility - Photon Flux | Professional WordPress repair service, global reach, fast response](https://www.361sale.com/wp-content/uploads/2024/03/image-381-1024x585.png)
Notes on PHPCompatibility and PHPCompatibilityWP versions.
Currently, the stable version of PHPCompatibility is9.3.5Recent sniffingis part of the upcoming release 10.0.0. Release. The current stable version of PHPCompatibilityWP is2.1.4
When PHPCompatibility version 10.0 is released, PHPCompatibilityWP version 3.0 will also be released, which will be dependent on PHPCompatibility version 10.0.
In the meantime, you can install the dev-develop branch of PHPCompatibility to run PHPCS and add the cutting-edge PHP 8 sniffing functionality before the release of PHPCompatibility version 10.0.0, for more information, please see here.WordPress VIP DocumentationThe
To do this, run the following command to specify an alias for the dev-develop branch of PHPCompatibility:
composer config minimum-stability devcomposer require --dev phpcompatibility/phpcompatibility-wp:"^2.1"composer require --dev phpcompatibility/php-compatibility: "dev-develop as 9.99.99"composer config minimum-stability dev composer require --dev phpcompatibility/phpcompatibility-wp:"^2.1" composer require --dev phpcompatibility/php-compatibility: "dev-develop as 9.99.99"composer config minimum-stability dev composer require --dev phpcompatibility/phpcompatibility-wp:"^2.1" composer require --dev phpcompatibility/php-compatibility: "dev-develop as 9.99.99"
These commands move thedevelop
The PHPCompatibility branch aliases to the 9.x version within the range allowed by PHPCompatibility and sets PHPCompatibilityWP to install the latest stable 2.1 version.
Once PHPCompatibility 10 and PHPCompatibilityWP 3 are released, it should be possible to update the PHPCompatibilityWP version constraints to "^3.0", which will depend on the PHPCompatibility version 10 This will depend on the version 10 of PHPCompatibility.
After installing all these, you can run the PHPCompatibilityWP tool on the plugin file.
The recommended approach is to run PHPCompatibilityWP for a specific base version of PHP. in this example, you can do this by setting thetestVersion
The runtime variables are set to run it for PHP 7.4 and higher.7.4-
The
. /vendor/bin/phpcs --runtime-set testVersion 7.4- -p wp-learn-php-compatibility.php --standard=PHPCompatibilityWP. /vendor/bin/phpcs --runtime-set testVersion 7.4- -p wp-learn-php-compatibility.php --standard=PHPCompatibilityWP. /vendor/bin/phpcs --runtime-set testVersion 7.4- -p wp-learn-php-compatibility.php --standard=PHPCompatibilityWP
You will see this output:
W 1 / 1 (100%)FILE: /Users/jonathanbossenger/wp-local-env/sites/learnpress/wp-content/plugins/wp-learn-php-compatibility/wp-learn-php- compatibility.php---------------------------------------------------------------------------------------------------------------------------------- -------FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE---------------------------------------------------------------------------------------------------------------------------------- -------15 | WARNING | Use of deprecated PHP4 style class constructor is not supported since PHP 7.---------------------------------------------------------------------------------------------------------------------------------- -------Time: 33ms; Memory: 8MBW 1 / 1 (100%) FILE: /Users/jonathanbossenger/wp-local-env/sites/learnpress/wp-content/plugins/wp-learn-php-compatibility/wp-learn-php- compatibility.php ---------------------------------------------------------------------------------------------------------------------------------- ------- FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE ---------------------------------------------------------------------------------------------------------------------------------- ------- 15 | WARNING | Use of deprecated PHP4 style class constructor is not supported since PHP 7. ---------------------------------------------------------------------------------------------------------------------------------- ------- Time: 33ms; Memory: 8MBW 1 / 1 (100%) FILE: /Users/jonathanbossenger/wp-local-env/sites/learnpress/wp-content/plugins/wp-learn-php-compatibility/wp-learn-php- compatibility.php ---------------------------------------------------------------------------------------------------------------------------------- ------- FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE ---------------------------------------------------------------------------------------------------------------------------------- ------- 15 | WARNING | Use of deprecated PHP4 style class constructor is not supported since PHP 7. ---------------------------------------------------------------------------------------------------------------------------------- ------- Time: 33ms; Memory: 8MB
Notice how the same error is reported as in the manual method, but this time it is more specific. It tells us exactly which line the error occurred on and what the error was.
Now we can fix the class constructor error.
caveat
One of the considerations when using a tool such as PHPCompatibilityWP is that it cannot detect every compatibility error.
For example, one of the other changes from PHP 7.4 to PHP 8.0 was the removal of the functionality used with the objectarray_key_exists()
Ratherproperty_exists()
A similar function should be used.
However, the PHPCompatibilityWP tool doesn't know about the variables you pass to thearray_key_exists()
is an array or an object, so it can't warn about it.
This is where automated manual tests will come in handy. When you run a test in a new PHP environment, the test will fail, allowing you to encounter possible problems. With logging enabled, you will see errors logged to the log file.
Ultimately, combining a tool like PHPCompatibility with the automated testing and manual testing processes we've discussed will allow you to ensure that your plugin is compatible with current and future versions of PHP.
Link to this article:https://www.361sale.com/en/6550
The article is copyrighted and must be reproduced with attribution.
No comments