PHP 7.1
June 23, 2020

Guide to PHP 7.1

PHP Development

PHP 7.1 was an incremental feature release, offering new language constructs, significant performance improvements, and lower resource utilization, making it a natural choice for fast-paced business-critical applications.

In this article, we look at PHP 7.1 features, performance comparisons, end of life, and why the PHP 7.1 release marked such a big change for PHP.

What Is PHP 7.1?

PHP 7.1 is the first feature release in the version 7 series and builds on both the performance improvements and features on PHP 7.0. In particular, it expands on variable typing features, adds features that make the language more consistent, and expands on several object-oriented and exception features.

PHP 7.1 Release Date

PHP 7.1 was released on December 1st, 2016.

PHP 7.1 EOL

Community support for PHP 7.1 ended on Dec 1, 2019; the last release was version 7.1.33, released on October 24, 2019. Zend by Perforce provides LTS support for 7.1 via its commercial Zend Server and ZendPHP products, and that support will continue until at least Dec 1, 2021.

PHP 7.1 Features

New feature for version 7.1 fell into X categories: type system additions, exception improvements, and list and array destructuring improvements, with a smattering of other small features thrown in for good measure.

When it comes to type system additions, these are largely initial steps towards union types, which will be released in the upcoming PHP 8 release later this year.

Nullable types

Prior to version 7.1, when declaring typehints for either parameters or return values, you needed to specify exactly one type. However, many built-in PHP functions will return “null” to indicate an error condition, and many userland object methods will return “null” to indicate no work was done. As such, the language added the idea of “nullable types”. These are a form of union type, and declared by adding a “?” before the type: “?Countable” indicates a Countable implementation or a null value; “?string” indicates a string or a null value. These can be used when declaring function or method arguments, or when declaring return values.

Void functions

The concept of a “void” return value has long been used when documenting functions and methods to indicate that the function or method does not explicitly return a value; they simply call “return;” somewhere, or never call it at all.

While PHP always evaluates such function and method return values as a “null” value, the language now allows you to declare “void” as the return value.
Static code analysis tools such as phpstan and psalm, when encountering a “void” return declaration, will flag when you have inadvertently returned a value from these functions and methods.

iterable pseudo type

Many functions and methods can consume or return values where a valid value could be either an array or something implementing Traversable (such as a generator, or an IteratorAggregate). Since the language does not support union types (yet!), developers have had to either use a type hint that is too strict or remove type hints entirely if they wish to allow either type.

PHP 7.1 introduces the “iterable” type, which is a pseudo-type acting as a union type of “array | Traversable”.

Closure from callable

Occasionally, you may want to define logic within a method marked “private” that you might want to expose to the user in the form of a callable. Unfortunately, the standard callback format, “[$object, $method]”, will raise an error when consumed, as the user would be calling on functionality forbidden due to the private visibility.

PHP 7.1 provides a new static method, `Closure::fromCallable()`, which allows you to wrap such callables in a Closure, automatically binding them to the instance. This then allows you to pass them around wherever they are needed.

Class constant visibility

When class constants were introduced, they were assumed to have public visibility. When visibility operators were introduced, however, they did not apply to class constants, which meant it was impossible to declare constants that were private to the class.

PHP 7.1 expands visibility operators to class constants. If none is provided, public visibility is assumed (as it is with other class members).

Catching Multiple Exception Types

When catching exceptions, you will often have the same exception handling logic for multiple exceptions that do not share the same inheritance trees. As such, prior to PHP 7.1, you would need to catch each type separately.

In another step towards union types, the “catch” operator now allows specifying multiple types when catching exceptions, in order to handle several types with the same code:

try {
} catch (FirstException | SecondException | ThirdException $e) {
}

 

Square bracket syntax for list()

The list() operator has been around for most of PHP’s lifetime, and is used to assign values from an array:

list($id, $name) = array(1, ‘Andi’);

Short array syntax was introduced in PHP 5.4, allowing for more concise declaration of arrays:

list($id, $name) = [1, ‘Andi’];

With PHP 7.1, you can now use the same short-array syntax anywhere you would have used “list()” previously:

$data = [
    [1, ‘Andi’],
    [2, ‘Zeev’],
    [3, ‘Rasmus’],
];
[$id, $name] = $data[1]; // $id = 2, $name = ‘Zeev’

foreach ($data as [$id, $name]) {
    // do something with $id and $name
}

 

Support for keys in list

Since arrays in PHP are used both for enumerables and maps, it would be useful for “list()” to support keys. As an example, consider the following set of data:

$data = [
    [‘id’ => 1, ‘name’ => ‘Andi’],
    [‘id’ => 2, ‘name’ => ‘Zeev’],
    [‘id’ => 3, ‘name’ => ‘Rasmus’],
];

In such a scenario, we cannot guarantee the order in which each of the individual “$data” entries will occur, which makes using “list()” to assign items to values error-prone.

To solve this problem, PHP 7.1 introduces the ability to pull values by key name:

[‘id’ => $id, ‘name’ => $name] = $data[1]; // $id = 2, $name = ‘Zeev’
foreach ($data as [‘id’ => $id, ‘name’ => $name]) {
    // do something with $id and $name
}

 

PHP 7.1 Performance

PHP 7.1 builds on the performance improvements first introduced in version 7.0, providing a small bump. Most CMS and eCommerce solutions will see a 1-2% improvement, while applications build closer to PHP, particularly those using frameworks, may see as much as 7% improvements.

Additional Resources

Get Long Term Support for PHP 7.1

Zend helps businesses with planning, managing, and maintaining their PHP applications on end-of-life PHP versions. Take advantage of frequent bug fixes and security updates with Zend PHP consulting services.

GET YOUR PHP SUPPORTED