Upcoming features of PHP 7.1 : speed is impressive againUpcoming features in PHP 7.1

The time to meet and greet PHP 7.0 is not so distant, and PHP 7.1 is already knocking at the door ! Since last year, the PHP group has been busy adding features, fixing old ones and keeping the new version as fast as possible : yet, PHP 7.1 managed to have an impressive list of new features. The RFC web site (https://wiki.php.net/rfc) has been voting a lot, and coding even more. Let’s see what are the upcoming features of PHP 7.1.

This is the first part of our series about last features in PHP 7.1. See ‘Upcoming features in PHP 7.1 (part a)‘ and ‘More upcoming features in PHP 7.1 (part b)‘ and ‘last features in PHP 7.1 (part c)

list() modernized

The venerable list() function (excuse my French, language structure) received a lot of development, and has been upgraded nicely.

<?php
// PHP 7.0 
$array = [0 => 'a', 1 => 'b', 2 => 'c'] ;
list($a, $b, $c) = $array ;
// $a is 'a', $b is 'b', $c is 'c'
?>

First, it is now possible to mention offset, and not only rely on integer indexing to assign the values. This saves calling array_values() on the left operand, and other array manipulations to make sure the order of the index are the same as the variables in the list() call.

<?php
// PHP 7.1
$array = [0 => 'a', 1 => 'b', 2 => 'c'] ;
list(1 => $b, 2 => $c, 0 => $a) = $array ;
// $a is 'a', $b is 'b', $c is 'c'
?>

Also, it is possible to use any data container in the list() call : variables, array with offset, properties, array appends or static properties.

<?php
class foo {
    private $a, $b, $c;
 
    public function __construct(array $bar) {
        list(
            "a" => $this->a,
            "b" => $this->b,
            "c" => $this->c
        ) = $bar;
    }
}
?>

Again, this syntax is short, and replace a lot of similar-looking code.

Finally, list() itself has been shortened to use the PHP 5.4-introduced Array short syntax. All the goodness above is also available with simple brackets :

<?php
class foo {
    private $a, $b, $c;
 
    public function __construct(array $bar) {
        [   "a" => $this->a,
            "b" => $this->b,
            "c" => $this->c
        ] = $bar;
    }
}
?>

It is also possible to nest list() inside other call to list() : destructuring multidimensional arrays !

<?php
$points = [
    ["x" => 1, "y" => 2],
    ["x" => 2, "y" => 1]
];
 
list(list("x" => $x1, "y" => $y1), list("x" => $x2, "y" => $y2)) = $points;
?>

Finally, one is able to assign several the same value to different variables, simply by mentioning several times the same index, in the left part of the expression :

<?php

[ 4 => $a, 4 => $b, 4 => $c] = [ 4 => 5];

// $a, $b and $c, all, contain 5 
?>

Read the rest of the RFCs (List keys and short list syntax), which have a lot of examples.

Generalization of negative offset

When using many string functions, it is possible to mention a negative offset, that represents an offset starting from the last character of the string.

<?php
	echo substr("abcde", 1, 1) ; // display b
	echo substr("abcde", -1, 1) ; // display d
?>

This type of notation has been generalized, and is available to reach any character inside a string :

<?php
	echo "abcde"[1] ; // display b
	echo "abcde"[-1]  ; // display d
?>

The only place that is left unchanged is array interpolation inside a string : there, a negative number is still considered as a weird expression, and yields a fatal error :

<?php
	$a = "abcde" ;
	echo "$a[-1]";
?>

Parse error: syntax error, unexpected ‘-‘, expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

Void return type

Void was not a reserved keyword in PHP 7.0, but it managed to join the party, along with int, string, and array, although this is only available as return type, not as argument. The void return type has to be used when a function doesn’t return anything.
Skipping the return statement, or returning with no argument are valid : by omitting the returned values, the code means that the function has no significant return value. On the other hand, returning null is not accepted as returning void : since the code has an explicit return value, it has another meaning that void.

<?php
function foo ($arg) : void {
	return ;
}
?>

PHP checks for return values at compile time. Using void-returning function in expression is still possible : it would introduce too many backward incompatibilities to be worth enforcing.

Regex /e option is deprecated (again)

It was deprecated in PHP 5.6 for preg_replace, and is it now deprecated for mb_ereg_replace  and  mb_eregi_replace. And ext/mbstring has the same alternative called mb_ereg_replace_callback, than pcre with preg_replace_callback. It may lack the preg_replace_callback_array, though (if you don’t know it, go and learn)

Mcrypt is deprecated

« Let’s get rid of ext/mcrypt, which is abandonware and inhibits the growth of the language, as soon as humanly possible. », states the RFC. Starting with PHP 7.1, all mcrypt function raise E_DEPRECATED errors, and will be removed the next iteration.
If you need cryptography, use the ext/openssl. If you really need it, get ready to go to PECL to compile it.

More on PHP 7.1 next week

That is enough for a first part. We’ll be back next week with more on PHP 7.1, covering nullable types, class constant visibility, octal and invalid string arithmetics, and more !