Is my code compatible with PHP 5.4 or 5.5?

Do you want to know if your code is compatible with that latest version of PHP? Do you need to upgrade your existing PHP website to a newer version of PHP? Do you want to see if your legacy PHP code is using any features that have been removed or deprecated in later versions of PHP? Imagine you have a PHP website that was written when PHP 5.1 was the current version. You are now in a situation where your web host is going to upgrade their server to PHP 5.4 and no longer support version 5.1. You need to migrate your existing code to ensure nothing will break when the upgrade occurs. How can you accomplish that? I have been working with PHP for 8 years and am keen on using the latest stable versions because of the performance, memory and security improvements and the latest useful features that are continually introduced. I’d like to introduce you to a tool we use at the Nerdery to check our PHP code conformance as well as a tool to determine whether an existing codebase is compatible with newer versions of PHP.

Changes in PHP version 5.4 and 5.5

What are some of the removed and deprecated features in PHP versions 5.4 and 5.5? In PHP 5.4, there are a few backward incompatible changes to consider. The most notable are:

  • Removal of magic quotes
  • Removal of safe_mode
  • Call time pass by reference has been removed
  • New reserved keywords: trait, callable, insteadof
  • register_globals is removed
  • Third parameter of ob_start has changed from a boolean to an integer type
  • The following functions have been removed: define_syslog_variables, import_request_variables, session_is_registered, session_register, session_unregister, and several mysqli_ functions.
  • Removal of the sqlite extension

There are not as many backward incompatible changes in version 5.5:

  • Removal of the following functions: php_logo_guid, php_egg_logo_guid, php_real_logo_guid, zend_logo_guid

For more information, please review the PHP 5.4 Migration page and the PHP 5.5 Migration page.

The Approach for Upgrading

When upgrading your website to a new version of PHP, you need to be valiant in testing every aspect of the site to ensure that the removed and deprecated features do not cause the functionality to change or break. You have a couple of approaches to test the site. Assuming you have a staging or test server, you can install the application on a server with the upgraded version of PHP and then perform the following:

  1. Run the project’s unit or functional tests. It you have unit tests or functional tests already written, this would be a great way to check what is broken based on the upgrade. The results of tests will indicate what changes you need to make to get the application working with the new version of PHP. Of course this will only work if tests have already been written to verify the functionality of the site.
  2. Use the site, trying to click on every link and perform every action and note any errors encountered, also checking the log file to find error messages related to the upgrade. This can be very time consuming and may be impossible to get a good coverage of the site’s pages.
  3. Run an automated tool that can detect the use of any deprecated or removed features in the codebase. To accomplish this step I’d like to introduce you to a tool called PHPCompatibility.

Enter PHP_CodeSniffer

PHPCompatibility is a standard created for a PHP Code analysis tool called PHP CodeSniffer. CodeSniffer is a tool from Squizlabs that is used to detect violations of code formatting standards in PHP files. We use it at the Nerdery to ensure our PHP code that we write conforms to our defined formatting standards. The way CodeSniffer works is it reads the PHP code and without executing it, tokenizes it into the various PHP tokens that represent different entities and types, for example a string is the token T_STRING and the word function is a T_FUNCTION token and a left parenthesis is T_OPEN_PARENTHESIS. Once the sniffer has created a map of all the tokens in the file, it uses its defined standards to compare the position and order of the tokens to ensure they appear in patterns that conform to the standards. PHP CodeSniffer comes with several different standards, but you can also create your own by extending its standards classes or making your own classes.

Install PHP CodeSniffer

You can install PHP CodeSniffer with PEAR, but I would recommend installing it with Composer. First install composer if you haven’t already. Then you can install PHP CodeSniffer using the following command:

composer global require "squizlabs/php_codesniffer=1.4.*"

This will install PHP CodeSniffer in ~/.composer/vendor/squizlabs/php_codesniffer. Now you need to make sure that your PATH is updated to include the global composer vendor bin directory by running the following command:

export PATH=~/.composer/vendor/bin:$PATH

Now check that PHP CodeSniffer runs by running this command:

phpcs -i

This command will output a list of installed coding standards. The standards are located in the directory ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards. The default coding standards that come with PHP CodeSniffer are Squiz, MySource, PSR2, Zend, PEAR, PSR1 and PHPCS. You can create your own if you’d like to. What we are going to do is install a standard specifically created to detect use of backwards incompatible code.

Is My Code Compatible?

Now we need to download the PHPCompatibility code standard and install it so PHP CodeSniffer can use it. The standard is created by Wim Godden and is available here: https://github.com/wimg/PHPCompatibility Install it anywhere by cloning the git repo with the following command:

git clone https://github.com/wimg/PHPCompatibility.git

Now you need to symlink (on Mac, *nix) or copy (on Windows) that directory to ~/.composer/vendor/squizlabs/php_codesniffer/CodeSniffer/Standards/PHPCompatibility, so PHP CodeSniffer is aware of it as a coding standard. Now when you run phpcs -i, you should see PHPCompatibility in the list of coding standards. To run the codesniffer, will we use the command phpcs. Go into the directory where your project files reside and run the command with the argument --standard=PHPCompatibility. The tool will run and as output show the problems detected in each file and which line the problem was found. Use the argument --extensions=php,inc,phtml to limit the sniffer to only inspect files with those extensions. It will make the script run faster, especially if there are a lot of js files in the codebase. From the error messages you can know if your code is incompatible with version 5.3, 5.4 or 5.5. Example output:

$ phpcs --standard=PHPCompatibility --extensions=php,inc,phtml .
FILE: /var/www/example-project/index.php
--------------------------------------------------------------------------------
FOUND 4 ERROR(S) AFFECTING 4 LINE(S)
--------------------------------------------------------------------------------
  3 | ERROR | The use of function session_register is discouraged in PHP
    |       | version 5.3 and forbidden in PHP version 5.4 and forbidden in
    |       | PHP version 5.5; use $_SESSION instead
 29 | ERROR | Function name, class name, namespace name or constant name can
    |       | not be reserved keyword 'trait' (since version 5.4)
147 | ERROR | The use of function mysqli_fetch is forbidden in PHP version 5.4
    |       | and forbidden in PHP version 5.5; use mysqli_stmt_fetch instead
248 | ERROR | Using a call-time pass-by-reference is prohibited since php 5.4
--------------------------------------------------------------------------------

Time: 0 seconds, Memory: 1.25Mb

Because PHP CodeSniffer is a static code analyzer, it does not actually execute the code and so it will not detect all the problems that could be caused due to an upgrade of PHP, but it will help you solve a majority of the problems.

I am a web developer working in PHP, JavaScript, Python and Scala.

Filed under Tech Tips

3 Responses to “Is my code compatible with PHP 5.4 or 5.5?”

  1. aditya menon  on November 19th, 2013

    Wow, using PHPCodesniffer to check for compatibility issues… awesome idea!

    Reply

  2. Aaron Dwyer  on November 19th, 2013

    Thanks for explaining that so clearly. I’m moving from 5.3 to 5.4 and have a lot of websites to check. This should make the upgrade far easier. Aaron

    Reply

  3. mugen  on December 2nd, 2013

    Very nice Idea thancks

    Reply

Leave a Reply