How to Upgrade From PHP 5.X to PHP 7 (Cheat Sheet)

by Ahmed Khan  March 15, 2016

If you are a regular to the Cloudways blog, you must be knowing that after repeated trials and errors, PHP 7 was integrated within the cloud platform. You must be well aware that PHP 7 is the next big thing in terms of programming languages. It is twice as fast from the previous versions, and more robust as compared to its predecessors.

php-7-cheat-sheet

A lot of our users have chimed in and asked us about having issues in upgrading their websites to the latest version of PHP. I am going to share with you how you can upgrade your website to PHP 7. But before that, I would like to bring to attention some points regarding the upgrade to make sure that all your bases are covered.

This post will cover the most important things to check on your site before upgrading to PHP 7 from PHP 5.5 and PHP 5.6. Let’s get started:

Deprecated Features

The first thing to keep in mind while upgrading to PHP 7 is to make sure that you are not using any of the deprecated methods or extensions. Some of the most commonly used methods and extensions are:

Constructors With Name Same as Class

PHP 4 style constructors are now deprecated, i.e., if your class has a constructor having the same name as your class name, then it is now deprecated in PHP 7. However, if you have made your constructor with __construct(), then it will be unaffected.

Deprecated

How to Fix It

The easiest way to fix this is to change all the constructor names to function __constructor(). And all the things will remain same.

Change in Password_hash() Function

If you are using PHP password_hash() method with custom salt to encrypt passwords in your site, than you need to remove custom salt since salt options has been deprecated in PHP 7. You need to use password_hash() without salt options. And if you are using password_hash() without salt options, than you don’t have to worry about it.

Deprecated

How to Fix It

For all the new users, you should start using password_hash() without salt options. All your old passwords can still be decrypted by using password_verify() .

Calling Static Methods

The methods which are not declared as static and are called as a static method is now deprecated. Only those functions which are declared as static can be called as static methods.

Affected

How to Fix It

For all the functions which you are calling as static methods, you can declare them as static by adding static before the functions leaving the rest of the code as it is.

Using MySQL_ To Handle Database Queries

If you are still using mysql_ to connect to your database and handle your database queries, than you should move to mysqli_ or PDO to handle your database queries since mysql_ is fully deprecated in PHP 7. If you continue using, then you will get the following error: Call To Undefined Function.

Conclusion

When you have signed up to the new and improved Managed PHP Hosting with Cloudways using the PHP 7, you need to be sure that your website is also compatible with the latest PHP version. To achieve that compatibility, you need to keep the above mentioned key points in your mind while you are upgrading your site to PHP 7. You can check out more information about it here. I would say that you also checkout the

I would say that you should also checkout the functions which have changed in PHP 7. Make sure your PHP based website is upgraded properly so that you can get the maximum benefit of your hosting at one the best managed cloud platforms, Cloudways PHP 7 Hosting Platform.

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...

  • Thank you Achmed for the share. Interesting PHP times ahead.

  • Samuel Lawson

    Thank you. Really useful information.

  • One other thing to note: If you are running any non-packaged extensions (such as mssql’s pdo driver) make certain the extension supports php7 before upgrading.