PHP7 is here. Well, the final release hasn’t been released yet, but next 12th November is scheduled to be released so maybe when you are reading this post the stable final release might be released. And, what does this new language version provide? The killer feature promised is performance. Zend has published the next infographic where you can see how they announce a 2x performance improvement. This might be a half of servers, software twice faster, so it is a considerable enhancement for the whole PHP ecosystem.

Performance

Zend did also publish this infographic showing the performance boost in PHP frameworks widely used, such as Magento or Drupal. This infographic shows how PHP7 is capable of serving more requests per second than even HHVM, Facebook’s project to improve PHP performance. At the bottom of the infographic, it is shown how PHP is the fastest dynamic language resolving Mandelbrot fractals problem. So, we have again another reason to use PHP, performance.

New features

PHP7 also provides new features, that have been added to the language to make it more modern. Syntactic sugar is always a good thing, that is why we are going to enumerate some of these new features in the language.

Spaceship operator

In the first place, we are going to explain the weird new feature, the Spaceship operator (<=>). This operator compares values returning what is expected by comparators, -1 if the first operand is less than the second, 0 if both operands are equal and 1 if the first operador is greater than the second.

<?php
$a = 1;
$b = 2;

echo $a <=> $b;

This operator is not a huge boost, but it is another weird thing more to the language. The only use case that comes to my mind is to use it inside anonymous functions used by sorting functions, like usort.

A language with better typing

PHP is a dynamic language, but sometimes it is useful to be able to define the type of a function argument or which type a function returns. In previous PHP versions, only class types were allowed to be defined in function arguments. PHP7 will allow also scalar types to be hinted in function arguments. It also adds the possibility to define which type a function returns. Be ready to have a better autocompletion support for the language in IDEs, because I suppose almost all PHP7 frameworks will make an extensive use of this new feature.

Coalesce operator

This flow structure has been the most written piece of code during my professional PHP life.

<?php

$x = array(
/* $_POST values */
);

$value = isset($x['value']) ?
    $x['value'] : null;

Don’t you agree? Haven’t you ever used this flow structure? To avoid a Notice when you don’t check if the key exists in an array, you are forced to write this piece of code. Some developers used the @ operator, that suppresses warnings generated by the statement surrounded by it. That is not a solution, that was just hiding an error.

<?php
$x = array(
/* $_POST values */
);

$value = @$x['value']);

In PHP7, we will be able to use the coalesce operator that will allow a neater way to solve this problem.

<?php
$x = array(
/* $_POST values */
);

$value = $x['value']) ?? null;

Group use declarations

From PHP5.3 forward, PHP has namespaces. If you use classes from another namespace, the use statement allows to use simply the class name instead of its full namespace name. Before PHP7, you had to write a use statement for each class. With PHP7, group use declarations can be used, writing only one line per package making it clearer what classes and namespaces are being used.

<?php
// Before
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as AliasClassC;

// Now
use some\namespace\{ClassA, ClassB, ClassC as AliasClassC};

Integer division

intdiv function will provide a way to execute the integer division without any trick.

<?php
// Before
$a = floor(4 / 3);

// Now
$a = intdiv(4, 3);

Expressions in constants

Now, constants will be able to store the result of an expression. Before, PHP threw an exception.

<?php

class A
{
    const DAY_SECONDS = 24 * 60 * 60;
}

Backwards compatibility

The biggest of problems with new major language versions, it’s cool to introduce new features and huge improvements but normally these features introduce BC breaks (backwards compatibility breaks). So, if a project is not under automatic testing or, at least, has a manual procedure established to know it does what is expected, we have a great problem if we have to migrate it to a newer language major version.

Regarding PHP7, it has been promised it won’t be a pain to migrate from PHP5 to it. Even though, some changes are worth to take a look before moving on.

Exception handling changes

The exception hierarchy has been modified. In PHP7 we will have exceptions and errors. Some structures that threw an uncatchable fatal error, now will throw an Error that could be catched with a try/catch statement, because this Error class will implement the Throwable interface that will be the new way to catch any exception or error in a block of code.

This will allow catching errors that before stopped the program flow, or simply triggered unmeaningful warnings, like the division by zero. Now, we can even catch a division by zero error and do something special about it.

New Objects cannot be assigned by reference

In PHP7, an object created with a new statement cannot be assigned by reference in the same statement to a variable. This piece of code was widely used in previous PHP versions because of some performance reasons, so if you are migrating a legacy project or you’re used to use this piece of code in your applications, it’s a good moment to stop using it.

<?php

$a = &new \stdClass();

More BC issues

The explained issues are the most important to me, but you can find other BC issues like removed functions in the official migration link or in this Github repository with examples.

PHP7 is here to make the Web better

In my opinion, it is a new version with little breaking compatibility changes, something positive, but it this mantra has also made impossible to do more ambitious changes to the language that might have considerably improved the language. But I suppose PHP mantainers don’t want a Python3 happening in PHP.

Other features I miss are more types and functions for the PHP internals. A decimal type would be great to do fixed numeric operations, something basic for high-level languages. So, to finish with this post, developers must keep updated, so it is important to know which are the new features provided by this new version because sooner or later this version will become the standard. And the sooner the better. PHP7 has performance and behaviour improvements that can’t be skipped. PHP7 is here to make the Web better.