Programming languages are used to create all the software we use, and the web is no exception. The most commonly used web languages are HTML, CSS, and our topic of the day: PHP. Hypertext Preprocessor (or PHP) is a server-side language, and allows users to perform actions on webpages and have the server deliver a response. A common use for PHP is creating website forms, where the user gives a name, email address, and message. This is then sent to the server, where it can forward it via email, or store the information in a database for later use.

In this guide, we will look over the new changes in PHP 7, and highlight any things you should be aware of when moving from PHP 5.6.x. This will include deprecated functionality, changes in the syntax (code formatting rules), and more.

We will cover the most important changes in detail, with a link to the full user documentation on the PHP website at the end of this article.

Prerequisites for upgrading to PHP 7

While we detail the most important changes in PHP 7, you will need the following before attempting an upgrade:

• A working knowledge of PHP. You can learn more about PHP scripting with this Fasthosts guide

• If looking to migrate from PHP 5.6.x to PHP 7, you can follow this full migration guide on the PHP website

• For older PHP versions, the PHP website has more guides. PHP 5.5 users can follow this guide to upgrade to PHP 5.6.x. You then have an upgrade path to PHP 7. There is no direct upgrade path for these older versions to PHP 7

Changes to error and exception handling

When you experience an irrecoverable fatal error, it normally results in a process or application crash. The way this is handled in PHP 7 is changing. Now, they are converted to exceptions, inheriting an Error class. This Error class also implements the Throwable interface, which is used to throw objects in PHP.

This presents a problem when using a custom error handler, due to exceptions now being thrown. You cannot use this Throwable interface in PHP 5.6.x, as this is a new introduction with PHP 7. In summary, you may need to alter existing PHP scripts to adhere with these class and interface changes.

Changes to internal classes

PHP 7 is standardising the handling of internal classes, as all internal classes will now throw an Exception, much in the same way user classes are forced to.

In PHP 5.6.x, internal classes would return a NULL response or unusable object when the constructor failed, offering little information with which to debug your scripts. This change will benefit troubleshooting and debugging, by offering more detail via thrown exceptions.

Server application programming interace (SAPI) modules (new feature)

FastCGI Process Manager (FPM) is used to manage server processes, and is particularly useful on servers which deal with heavy network traffic. In previous PHP versions, only IPv4 was supported. Now, FPM has full support for the IPv6 network protocol, bringing native modernised network protocol support to PHP.

Scalar type declarations (new feature)

The new scalar type declaration functionality has been implemented to augment PHP 5 items like class names, interfaces, array, and callable parameters. These declarations can be either coercive (the default), or strict, offering granular control.

Return type declarations (new feature)

Return type declarations are used to specify the permissible value types that can be returned from a function call. This helps to reduce processing overhead by omitting value types that are not needed.

You can find full details on this at php.net.

PHP 4 style constructors (deprecated)

If you are currently using PHP 4 style constructors, you will need to rewrite related scripts. PHP 7 has deprecated this functionality, with a full removal being planned in later releases.

This is being deprecated due to the performance improvements delivered by PHP 7. These older constructors lack optimisation which has been fixed with updated constructor methods, hence the upcoming removal. The PHP community also voted overwhelmingly to deprecate this functionality, as detailed here.

Password hash options (deprecated)

In previous versions of PHP, developers were able to generate their own password hash salts. The problem was these salts were often insecure, posing a security risk when used in live environments.

This means that the password_hash() function has been deprecated in PHP 7, requiring you to use alternative methods to generate hash salts.

Static calls to non-static methods (deprecated)

As a rule of thumb, a static method belongs to the class. It cannot belong to the instance. This is in stark contrast to non-static methods, which can access any static method without creating an instance of the class.

PHP is deprecating static calls to methods which are not declared static, helping users to avoid making calls that trigger exceptions or errors.

capture_session_meta SSL context option (deprecated)

Previously, secure socket layer (SSL) metadata was available through the capture_session_meta function. This has now been changed and made available via the stream_get_meta_data() function. For this reason, the old function has been deprecated, and will be removed in upcoming versions of PHP.

mcrypt (deprecated)

mcrypt is an extension for PHP that was created more than a decade ago. This extension has not seen development for an extended period of time, and existing bugs and security risks have not been fixed. For this reason, PHP 7 now classes mcrypt as abandonware, and is moving to deprecate and remove support for this extension.

This is being replaced by OpenSSL, an open-source alternative to the ageing mcrypt extension.

Summary

As you can see, there are numerous changes with PHP 7. We have covered the most important changes, but a full changelog is available on the PHP website here. It is recommended to read this fully, as you may need to make significant alterations to your PHP scripts to achieve compatibility with this new version. The incentive to upgrade would be PHP 7’s significant performance improvements, which help to improve the responsiveness and stability of your webpages.

Deliver PHP webpages with Fasthosts

PHP webpages offer rich web experiences, and Fasthosts offers rich website hosting services. We have dedicated Website Hosting service packages, which provide performant hosting servers for your websites via our award-winning Fasthosts Control Panel.

Get online faster with Fasthosts! Contact our sales team on 0808 1686 777, or via email at sales@fasthosts.co.uk to take the first steps.