14 december 2016

PHP 7.1 what to expect?

PHP 7.1.0 was released this month, so it's time for a roundup of the most important new features that were added in this release.


Catching Multiple Exception Types

It is now possible to catch several exception types with one single catch block. You can provide multiple exception classes separated by a pipe symbol.

try {
catch (RateLimitExceededException $e) {
  // do some exponential backoff
catch (InvalidCredentialsException |
  InvalidParametersException $e
) {
  // report an error

In previous versions, a catch block could only catch one exception type. You could use inheritance to try to work around the problem, but you don't always have that option. For example, when exceptions are thrown by third party library.


Nullable Types

Since PHP 7 you can use scalar type-declarations for functions and methods parameters. But this feature was lacking support for nullable types, which is now resolved. You can use nullable types by adding a "?" before the type declaration.

function printNumber(?int $a) {

printNumber(100); // int(100)
printNumber(null); // NULL | Before: TypeError: Argument 1 passed to printNumber() must be of the type integer, null given.

Before, PHP would throw an fatal error for passing NULL. 


Void Return Type

Another feature the new scalar type-declarations were missing, was the possibilty to have a function or method return no value. This was fixed by introducing the void return type.

function returnsNothing(): void { return; // valid }

Iterable type

PHP 7.1 introduced a new pseudo-type called iterable. It groups all types on which we can iterate like arrays and traversable data.


Support for keys in list function


is used to assign a list of variables from an array source. From now on you can specify the array key's to only assign specific array values to variables.

$array = [
  'foo' => 'hello',
  'bar' => 'wonderfull',
  'baz' => 'world',

  'foo' => $a,
  'baz' => $b
) = $array;

var_dump($a, $b);
// string(5) "hello"
// string(5) "world"

Generalize support of negative string offsets

Support for negative string offsets has been added to the string manipulation functions that were accepting offsets. For example:

echo $str[-2]; // => "e"

echo $str; // => "abc.ef"

Class Constant Visibility

Class constants were always public and could be accessed from outside the class. Sometimes you don't want to expose them to other classes. So now it is possible to specify the visibility (public, protected, private) of class constants. For example:

class Galaxy {
  private const ANSWER = 42;

In the previous versions, class constants were always public, which remains the default when you don't specify the visibility.


Too few arguments exception

In previous php versions you were allowed to call a function with lesser arguments than what it required, which could lead to unexptected behavior further down in your code. From PHP 7.1 this won't work and you will get an exception, this way you can handle these cases immediately and avoid any invalid input.

function greeting($name) {
  echo "Hi " . $name;
greeting(); // Fatal error: Uncaught ArgumentCountError: Too few arguments to function greeting(), 0 passed ...

Other features

These are the most important features. If you want to see all the changes made in PHP 7.1 make sure to check the official migration guide.


What's next?

The PHP community is already implementing features for the upcoming 7.2 release. These are a few of the RFC's that passed voting or are already implemented.

And see Tom van Looy's blogpost about PHP8's jit compiler.

If you would like to keep an eye on what rfc's are introduced for upcoming php versions or catch up with the discussion on the php mailinglist, then RFC watcher and are great resources to do so.

Now you're all update to date with the most interesting features, it's time to upgrade your servers and get hands on with php 7.1!