New features in PHP 7.1

The PHP development team announced PHP 7.1.0 on 01 Dec 2016. This release is the first point release in the 7.x series. There are a few features – like the void return type – which have been introduced. Below are a few new selected features in PHP 7.1.

Void functions

A void return type has been added. Functions declared with void as their return type must either omit their return statement altogether, or use an empty return statement. Like in previous versions null is not a valid return value for a void function.

A void function can return in two ways.

<?php
 
function should_return_nothing() : void {
    // Do something
    return; // valid
}
 
// OR
 
function should_return_nothing() : void {
    // Do something
    // valid
}

We cannot return like this.

function should_return_nothing() : void {
    // Do something
    return null; // Fatal error: A void function must not return a value
}

This will throw the following error.

Fatal error:  A void function must not return a value

Note that void is only valid as a return type, not as a parameter type:

function foobar(void $foo) { 
    // Fatal error: void cannot be used as a parameter type
}

Nullable types

Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark. This tells that besides the specified type, null can also be passed as an argument, or returned as a value, respectively. Nullable types are permitted anywhere type declarations are allowed but are subject to some inheritance rules.

function answer(): ?int  {
    return null; //ok
}
 
function answer(): ?int  {
    return 42; // ok
}
 
function answer(): ?int {
    return new stdclass(); 
    // Fatal error: Uncaught TypeError: Return value of 
    // answer() must be of the type integer or null
}
function say(?string $msg) {
    if ($msg) {
        echo $msg;
    }
}
 
say('hello'); // ok -- prints hello
say(null); // ok -- does not print
say(); // error -- missing parameter
say(new stdclass); //error -- bad type

Note: Parameters with a nullable type do not have a default value. If omitted the value does not default to null and will result in an error:

function f(?callable $p) {}
f(); // invalid; function f does not have a default

PHP’s existing semantics allow giving a null default value for a parameter to make it nullable and optional:

function foo_default(Bar $bar = null) {}
 
foo_default(new Bar); // valid
foo_default(null); // valid
foo_default(); // valid

Symmetric array destructuring

The shorthand array syntax [], introduced in PHP 5.4, may now be used to destructure arrays for assignments (including within foreach), as an alternative to the existing list() syntax, which is still supported.

Using the ‘short array syntax’ introduced in PHP 5.4 we can define an array as below.

<?php
// Creates an array containing elements with the values 1, 2 and 3, 
// and keys numbered from zero
$array = [1, 2, 3];
 
// Creates an array containing elements with the values 1, 2 and 3, 
// and the keys "a", "b", "c"
$array = ["a" => 1, "b" => 2, "c" => 3];

Similar to the array() syntax for constructing arrays, PHP has had a syntax form for assigning to variables from array elements, know as “destructuring”, since PHP 3.

<?php
 
// Assigns to $a, $b and $c the values of their respective array 
// elements in $array with keys numbered from zero
list($a, $b, $c) = $array;

With the new PHP 7.1 feature we can destructure and array like below.

<?php
 
// Assigns to $a, $b and $c the values of their respective 
// array elements in $array with keys numbered from zero
[$a, $b, $c] = $array;
 
// Assigns to $a, $b and $c the values of the array elements
// in $array with the keys "a", "b" and "c", respectively
["a" => $a, "b" => $b, "c" => $c] = $array;

This syntax is more concise, and like the [] alternative to array(), this new syntax does not resemble a function call.

Importantly, this syntax for destructuring an array means there is now symmetry between array construction and destructuring, which should make it clearer what the function of the syntax is:

<?php
 
// The two lines in each of the following pairs are equivalent to each other
 
list($a, $b, $c) = array(1, 2, 3);
[$a, $b, $c] = [1, 2, 3];
 
list("a" => $a, "b" => $b, "c" => $c) = array("a" => 1, "b" => 2, "c" => 3);
["a" => $a, "b" => $b, "c" => $c] = ["a" => 1, "b" => 2, "c" => 3];
 
list($a, $b) = array($b, $a);
[$a, $b] = [$b, $a];

Class Constant Visibility

Classes in PHP allow modifiers on properties and methods, but not constants. Class constant may be defined as public, private or protected and undeclared constants without any explicit visibility keyword are defined as public.

<?php
 
class ConstDemo
{
    const PUBLIC_CONST_A = 1;
    public const PUBLIC_CONST_B = 2;
    protected const PROTECTED_CONST = 3;
    private const PRIVATE_CONST = 4;
}

Support for negative string offsets has been added to the string manipulation functions accepting offsets, as well as to string indexing with [] or {}. In such cases, a negative offset is interpreted as being an offset from the end of the string.

In most PHP functions, providing a negative value as string offset means ‘n positions counted backwards from the end of the string’. This mechanism is widely used but, unfortunately, these negative values are not supported everywhere. It is hard for most PHP developers to know whether a given string functions accepts negative values or not, and need to regularly refer to the documentation. Such inconsistency in the usage of string functions has given PHP a bad name.

An example is strrpos() accepting negative offsets, while strpos() does not. The same with substr_count() rejecting negative offset or length, when substr() accepts them.

<?php
 
var_dump("abcdef"[-2]);
var_dump(strpos("aabbcc", "b", -3));
 
// output:
string (1) "e"
int(3)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>