PHP 7.1 is released – Featuring nullable types, class constants..

Posted on Posted in mamp, php

PHP 7.1 is now released – Featuring nullable types, class constants…

Today, the PHP team has released 7.1.0 that includes new features such as Nullable types, Void return type, Class constant visibility modifiers, and more. Here is a quick overview of some of the new features.

Nullable Types

This adds a leading question mark indicate that a type can also be null.


function answer(): ?int  {
    return null; //ok
}

function answer(): ?int  {
    return 42; // ok
}

function answer(): ?int {
    return new stdclass(); // error
}


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

Void Return Type

This requires that a function not return any value:


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

function lacks_return(): void {
    // valid
}

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

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


class Foo
{
    public function bar(): void {
    }
}
 
class Foobar extends Foo
{
    public function bar(): array { // Fatal error: Declaration of Foobar::bar() must be compatible with Foo::bar(): void
    }
}

Class constant Visibility

You now have the ability to set the visibility on class constants.


class Token {
    // Constants default to public
    const PUBLIC_CONST = 0;

        // Constants then also can have a defined visibility
        private const PRIVATE_CONST = 0;
        protected const PROTECTED_CONST = 0;
        public const PUBLIC_CONST_TWO = 0;

        //Constants can only have one visibility declaration list
        private const FOO = 1, BAR = 2;
}

Allow specifying keys in list()

Unpacking array elements into variables is a common operation in certain types of PHP code. For example, it is common to use an array as an “argument bag” when initialising an object:


name = $attributes["name"];
        $this->colour = $attributes["colour"];
        $this->age = $attributes["age"];
        $this->cuteness = $attributes["cuteness"];
    }
 
    // ...
}

Apparently ideal for this use-case, PHP has a language construct for unpacking multiple array elements into variables, namely list(), which is used like so:


list($first, $second, $third) = $someArray;

And is equivalent to:


$first = $someArray[0];
$second = $someArray[1];
$third = $someArray[2];

Catching Multiple Exception Types

When two different types of exceptions are handled the same way, it is sometimes required to duplicate the code of the catch statements.
For example :


try {
   // Some code...
} catch (ExceptionType1 $e) {
   // Code to handle the exception
} catch (ExceptionType2 $e) {
   // Same code to handle the exception
} catch (Exception $e) {
   // ...
}

And is equivalent to:


try {
   // Some code...
} catch (ExceptionType1 | ExceptionType2 $e) {
   // Code to handle the exception
} catch (\Exception $e) {
   // ...
}

For source downloads of PHP 7.1.0 please visit our downloads page, and For a full list of new features take a look at the release announcement and the changelog. Also, Amo Chohan has a great article highlighting all the changes in PHP 7.1.