PHP 7 - What's up, and what's new?

Update nov. 9 - Looks like the PHP 7 release date has been postponed due to some outstanding bugs and regressions. It should still be right around the corner, though.

PHP 7 overview

The next major version of PHP, PHP 7, should be right around the corner, with a planned release date on November 12th, according to the current release timetable. PHP 7 will include of a lot of performance improvements, and a range of new features. In thi of post I'll try to go through the major new stuff.

But, what about PHP 6?

You probably know that the current version is PHP 5.6, and if the next version is PHP 7, obviously PHP 6 was skipped. I won't go into details with this choice since I'd rather just forget about it.

But in short, first politics happened, then bullshit happened, and then finally they had a vote and moved on.

Cleanup

Since PHP 7 is a major version release, backwards compatibility breaks are allowed. BC breaks are never nice, so the dev team is trying to keep them at a minimum, but some BC breaks should be expected.

Deprecation of PHP 4 style constructors

PHP 4 introduced object oriented programming to PHP. Back in those days an object constructor was a method named like the class you wanted to create an object from.
class MyObject {
  protected $param;

  // Object constructor.
  function MyObject($param) {
    $this->param = $param;
  }
}

In PHP 5 the class name constructor was replaced with the new magic method __construct() as the recommended way to create objects.

class MyObject {
  protected $param;

  // Object constructor.
  function __construct($param) {
    $this->param = $param;
  }
}

PHP 4 style constructors is still available in PHP 5, unless you're using namespaces, in which case they are ignored and only the PHP 5 style constructors are available. This inconsistency can cause some confusion, and this is the main reason that the PHP 4 style constructors will be marked as deprecated in PHP 7 and removed in the following PHP 8. This means that in PHP 7 a class with a PHP 4 style constructor, but no PHP 5 style constructor will cause PHP to emit an E_DEPRECATED error.

Removal of SAPIs and extensions

PHP 7 also removes a bunch of SAPIs (Server APIs - PHP interfaces for module developers to extend the webserver) and extensions. I'll skip on the SAPIs as I doubt that change will affect me.

Two modules have been removed. The ext/ereg regex module which has been deprecated since PHP 5.3, and the ext/mysql extension, which has been deprecated since PHP 5.5. If you are using the ereg functionality, it is recommended that you switch to using pcre, and if you are still using the mysql functions, you should switch over to using either MySQLi or PDO, more info for choosing which of the two to use can be found in the manual.

Removal of other functionality

Besides the removed SAPIs and extensions, a bunch of other functionality will be removed, most notably probably being some mcrypt, iconv and mbstring functionality. For a full list, refer to the rfc.

PHP 7 Performance

Internally the Zend Engine has been completely refactored and rebuilt, a project known as PHPNG (next generation). This rewrite allows for a lot of improvements, most of which go way over my head (I'm a web developer, not a language developer), but you can find a lot of details on the wiki.

Some of the notable improvements includes improved hash tables and improvements to the internal representation of values and objects, which Nikita Popov has written some interesting articles about: <a href=https://nikic.github.io/2015/05/05/Internal-value-representation-in-PHP-7-part-1.html">part 1 and part 2.

For PHP developers, the most notable improvements will probably be the drastically improved performance, that Rasmus Lerdorf has been talking about.

Engine Exceptions

Until now when something went really wrong in a PHP script, it would cause either an error, or an exception. An error was an internal PHP things that would either kill the script, or at least output an error message, either to the user, but hopefully just to a log file, depending on your setup. An exception was a part of the script, it would stop the execution and bubble up through the call stack until it hit something designed to catch it, or kill the script if nothing would catch it.

An exception would change the script's execution flow, but could be mitigated in a properly designed application, while errors were a bit harder to handle on runtime. In PHP 7 the Exception hierarchy has been extended, and the error types that would previously stop the entire script execution, will be catchable, so the developer is able to handle the errors more gracefully. To keep backwards compatibility all exceptions still extend the Exception class, while the errors will extend a new Error class. To ensure both errors and exceptions can be handled together they will both implement a new Throwable interface, making it possible to make catch-all solutions. You can learn more about the new engine exceptions in this rfc, and about the new Throwable interface in this rfc.

Scalar type hints

PHP 5 introduced type hinting, the notion of allowing methods to expect it's parameters to be of a certain type, and throw a recoverable fatal error if the input didn't match the type hint. This was possible for objects and arrays. In PHP 7 it is also possible to type hint scalar types, ie. integers, floats, booleans and strings. If an input parameter doesn't match the hinted type, the default behaviour will be to convert the value to the type hinted type, but it's possible to turn on strict mode, in this case a TypeError Exception will be thrown if an input parameter has the wrong value. Strict mode will have to be set for each file where it should be activated. I'm really looking forward to this change, and will probably be writing a lot of strict files. I'll probably get more into this in a later post.

Return type hints

Another new thing that I think will be really awesome is return type hints. In the same way that methods can define which parameter types it expects as input, it will also be able to make a promise of which data type it will return. Declared return types will be included in PHP 7, but an rfc extending the concept by adding void return types is currently being voted on. Another cool extensions would be nullable types, the rfc is currently only a draft, but I'm hoping to see this for PHP 7.1. Return types is another thing I'm really looking forward to, and will probably write more about later.

Null coalesce

Null coalesce is a variant of the ternary operators, aka shorthand ifs. The new ternary operator ?? will also check if a value is null. Example:
$category = $_GET['category'] ?? 'cakes';
In human terms this means, $category should get the value of $_GET['category'] if it is set, otherwise, it should be 'cakes'. This could, of course, also be done using the existing ternary operator
$category = isset($_GET['category']) ? $_GET['category'] : 'cakes';
This is not a revolutionary addition, but rather some nice syntactic sugar.

Anonymous classes

PHP 5.3 introduced anonymous functions, which is unnamed functions defined during runtime. In PHP 7 the concept is expanded with anonymous classes. I believe their main purpose is for single use objects where stdClass might not be enough, but I'll have to work with it for real before I'll figure out their real purpose.

Group use

PHP 5.3 also introduced namespaces, logical separation of classes into groups. To use classes from other namespaces you can either use their fully qualified name:
namespace Foo;

class Bar { protected $baz;

public function __construct() { $this->baz = new \Xyz\Baz(); } }

or use the required class from it's namespace

namespace Foo;

use Xyz\Baz;

class Bar
{
  protected $baz;

  public function __construct()
  {
    $this->baz = new Baz();
  }
}

PHP 7 introduces grouped use declarations, so it's possible to include multiple classes from a namespace in a single use statement. This is done by stuffing the class names in curly braces. It's still possible to name individual includes

use Name\Space\{Foo as Xyz, Bar}

I'll let you decide whether this is nice syntactical sugar, or another way to build an unreadable mess, since I havn't fully decided yet.

Uniform variable syntax

The uniform variable syntax tries to make variable syntax more consistent. I don't have a full overview over all of the changes yet, but the RFC contains a lot of examples. This is a pretty important change to be aware of, since it might actually change the behaviour of, or just break, some of your running code since it changes how a lot of expressions are evaluated.

CSPRNG

PHP has always had functionality to generate pseudo-random data, the standard functionality just havn't been cryptographically secure (aka. random enough). PHP 7 introduces some new functionality for creating data that is even more random.

Further reading about PHP 7

In this post I've introduced a bunch of the major changes in PHP 7 that I believe will be the most impactful to m daily life as a developer. There is a lot of more stuff being added and removed, and a full list is available here.

Do you have any change that you look forward to more than the rest of PHP 7? Or do you see anything coming up in one of the following versions that makes everything even cooler? Leave your answer in a comment.