PHP 7: Release Candidate 2

The next version of the popular open-source scripting language is set to be released in early November 2015 – just a few months from time of writing! The second release candidate has been set free with a few bug and security fixes.

The upcoming version includes new features such as full and consistent 64-bit support across platforms (which also adds support for larger file sizes), removal of a number of old/dead SAPI extensions (including deprecation of mysql in favor of mysqlnd/mysqli or PDO), added support for null coalescing (also known as the isset ternary) and combined comparison operators, return and scalar type declarations, and anonymous classes.

However, the biggest change was speed. PHP 7 benchmarks are shown to be up to twice as fast as the current 5.6 version and nearly as fast as HHVM. My own benchmarks of RC 1 have shown an average 95% increase in performance on a 64-bit Windows 7 machine running benchmark scripts. The biggest performance gain was seen in string manipulation which is what many scripts rely on. However, perception is the real key and there it does not disappoint as either. Local installs of WordPress, upon first run, have gone from ~4 seconds to ~2 seconds. Subsequent runs are nearly instantaneous (1 second or less). Running them in production on Linux servers will definitely be even faster.

Backward In-Compatibility

While the majority of scripts wrote to support the PHP 5.x branch will work perfectly fine on PHP 7, there are some fixes that remain. Most notably, a number of scripts still use the old PHP 4-style constructors. They are being phased out and issue a deprecation warning. Instead of using a method with the same name as the class, you should instead create a method called “__construct” (two underscores followed by “construct”) to create constructor methods. The old mysql (mysql_* functions) are in the same boat. The old mysql extension is deprecated and will be removed in upcoming PHP versions. Scripts need to be updated to use mysqlnd (MySQL Native Driver) through the use of mysqli (mysqli_* functions) or PDO.

The other major change some scripts may run into trouble with, only if Object-Oriented Programming (OOP) is used, is how class variables are returned. According to the upgrade document:

* Indirect variable, property and method references are now interpreted with left-to-right semantics. Some examples:

To restore the previous behavior add explicit curly braces:

Errorpocolypse

The last major difference which may confuse developers is with error display and handling. The entire error handling system has been ported to use exceptions and will now only show an error in a few cases. Most errors, including most fatal errors, will now issue exceptions which do not display like errors do in version 5. You may run into an empty or blank white display or have a partially-rendered page. This is normal for PHP 7. What is happening is that a fatal error was encountered but instead of showing the typical error message and backtrace it simply halts with most scripts as they are currently written. Instead of an error an exception is thrown. A number of developers are not used to using exceptions and will become confused.

In a future post (to be linked here) I will go more in-depth into the changes made to the error system in PHP 7 as well as how to handle them (now using exceptions) and get more information from them while developing scripts.

Leave a Reply

Your email address will not be published. Required fields are marked *