New features

Nullable types

Types can now be made nullable, enabling for either the specified type or null to be passed to or returned from a function. This is done by using a question mark to prefix a type to denote it as nullable.

<?php
function test(?string $name)
{
    
var_dump($name);
}

Результат выполнения данного примера:

string(5) "tpunt"
NULL
Uncaught Error: Too few arguments to function test(), 0 passed in...

Void functions

A void return type has been introduced to amalgamate the other return types introduced into PHP 7. Functions declared with void as their return type can either omit their return statement altogether, or use an empty return statement. null is not a valid return value for a void function.

<?php
function swap(&$left, &$right) : void
{
    if (
$left === $right) {
        return;
    }

    
$tmp $left;
    
$left $right;
    
$right $tmp;
}

$a 1;
$b 2;
var_dump(swap($a$b), $a$b);

Результат выполнения данного примера:

null
int(2)
int(1)

Attempting to use a void function's return value simply evaluates to null, with no warnings are emitted. The reason for this is because warnings would implicate the use of generic higher order functions.

Symmetric array destructuring

The shorthand array syntax ([]) may now be used to destructure arrays for assignments (including within foreach). This pattern matching ability enables for values to be more easily extracted from arrays.

<?php
$data 
= [
    [
'id' => 1'name' => 'Tom'],
    [
'id' => 2'name' => 'Fred'],
];

while ([
'id' => $id'name' => $name] = $data) {
    
// logic here with $id and $name
}

Class constant visibility

Support for specifying the visibility of class constants has been added.

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

iterable pseudo-type

A new pseudo-type (similar to callable) called iterable has been introduced. It may be used in parameter and return types, where it accepts either arrays or objects that implement the Traversable interface. With respect to subtyping, parameter types of child classes may narrow a parent's iterable type to either array or an object (that implements Traversable). With return types, child classes may widen a parent's return type of array or an object to iterable.

<?php
function iterator(iterable $iter)
{
    foreach (
$iter as $val) {
        
//
    
}
}

Multi catch exception handling

Multiple exceptions per catch block may now be specified using the pipe character (|). This is useful for when different exceptions from different class hierarchies are handled the same.

<?php
try {
    
// some code
} catch (FirstException SecondException $e) {
    
// handle first and second exceptions
}

Support for keys in list()

list() now enables for keys to be specified inside of it. This means that it can now destructure any type of arrays (likewise the shorthand array syntax).

<?php
$data 
= [
    [
'id' => 1'name' => 'Tom'],
    [
'id' => 2'name' => 'Fred'],
];

while (list(
'id' => $id'name' => $name) = $data) {
    
// logic here with $id and $name
}

Support for Negative String Offsets

Support for negative string offsets has been added to all string-based built-in functions that accept offsets, as well as to the array dereferencing operator ([]).

<?php
var_dump
("abcdef"[-2]);
var_dump(strpos("aabbcc""b", -3));

Результат выполнения данного примера:

string (1) "e"
int(3)

Support for AEAD in ext/openssl

Support for AEAD (modes GCM and CCM) have been added by extending the openssl_encrypt() and openssl_decrypt() functions with additional parameters.

Convert callables to `Closure`s with Closure::fromCallable()

A new static method has been introduced to the Closure class to allow for callables to be easily converted into Closure objects.

<?php
class Test
{
    public function 
exposeFunction()
    {
        return 
Closure::fromCallable([$this'privateFunction']);
    }

    private function 
privateFunction($param)
    {
        
var_dump($param);
    }
}

$privFunc = (new Test)->exposeFunction();
$privFunc('some value');

Результат выполнения данного примера:

string(10) "some value"

Asynchronous signal handling

A new function called pcntl_async_signals() has been introduced to enable asynchronous signal handling without using ticks (which introduce a lot of overhead).

<?php
pcntl_async_signals
(true); // turn on async signals

pcntl_signal(SIGHUP,  function($sig) {
    echo 
"SIGHUP\n";
});

posix_kill(posix_getpid(), SIGHUP);

Результат выполнения данного примера:

SIGHUP

HTTP/2 server push support in ext/curl

Support for server push has been added to the CURL extension (requires version 7.46 and above). This can be leveraged through the curl_multi_setopt() function with the new CURLMOPT_PUSHFUNCTION constant. The constants CURL_PUST_OK and CURL_PUSH_DENY have also been added so that the execution of the server push callback can either be approved or denied.

Коментарии

Автор:
Note that declaring nullable return type does not mean that you can skip return statement at all. For example:

php > function a(): ?string { }
php > a();
PHP Warning:  Uncaught TypeError: Return value of a() must be of the type string or null, none returned in php shell code:2

php > function b(): ?string { return; }
PHP Fatal error:  A function with return type must return a value (did you mean "return null;" instead of "return;"?) in php shell code on line 2
2018-04-06 14:20:50
http://php5.kiev.ua/manual/ru/migration71.new-features.html
Автор:
<?php
function swap( &$a, &$b ): void
 
{ [ $a$b ] = [ $b$a ]; }
?>
2018-04-09 13:36:56
http://php5.kiev.ua/manual/ru/migration71.new-features.html

    Поддержать сайт на родительском проекте КГБ