Developer Blog

  • Blog
  • /
  • Migrating to PHP 7
By Dracony on 17 November 2015

A few days ago I switched a server hosting a bunch of my personal websites to PHP 7. Some of those rather old and spanning a wide range of frameworks and CMSes. These are some of my thoughts for those debating whether they should switch to PHP 7 or wait some more.

Firstly, I know there are people who don’t consider a stable release truly “stable” until it has been out for a while, waiting for errors to be found and patched. And from what I’ve seen so far, trying out every release candidate as it was released, it is definitely safe to start using PHP 7 as soon as it is out. I never had an unexpected behavior, a random segmentation fault, or anything that was not my fault. Even though it is a major version it does not cause much backwards compatibility breaks, so in general terms you can treat is just as if it was PHP 5.7, just significantly faster.

The performance does shine, almost unbelievably so. To give you an example a simple PHPixie app performed up to 3 times faster, almost matching the speed of Phalcon on PHP 5.6. A recent report by Google claimed that even a 10% decrease of performance results in a hefty percentage of lost customers. Following this logic if you manage to double your performance just by upgrading your PHP, you get more sales for free. Mention this to your manager when trying to convince them to switch. Nothing persuades better than sales numbers.

Some notes

The mysql extension is not available anymore, so if you never switched to PDO or mysqli this is going to be a problem. Fortunately a simple replace of mysql_ function calls with mysqli_ solves this perfectly.

E_STRICT errors have been reclassified to other error types. So if you had those errors disabled they will start popping out as warnings and notices. For example calling non-static methods statically is now E_DEPRECATED and caused me a lot of troubles with Joomla 2.5 which for some reasons does this in quite a few places. Also signature mismatch during inheritance will yield a warning. Surprisingly all of the Wordpress sites with quite a lot of plugins installed worked pretty well on the first try, the functional nature of Wordpress made it resilient to E_STRICT changes entirely.

foreach operates on a copy of the array. So if you modify the array while iterating over it, those changes will not be available inside that loop anymore.

Another troublesome change is that $foo->$bar['baz'] is now interpreted as ($foo->$bar)['baz'] unlike the PHP 5 $foo->{$bar['baz']}. This is a weird case, but I ran into it a few times in some Wordpress plugins.

Keep in mind that not all your PHP extensions may support PHP 7 yet, sadly I can no longer use the excellent XCache opcode cacher that worked perfectly for me for so many years.

Other than that the breaking changes were rather tame and you are very unlikely to run into them. If you want to be thorough a full list is available here.

All in all it took me about 5 hours to update all my websites to PHP 7. The process is really straightforward and packages are already available for all major Linux distributions, so there is really no excuse not to upgrade.

comments powered by Disqus