📜 ⬆️ ⬇️

PHP 7.1: Overview of New Features

image On Habré, there was already a translation with a review a few months ago, but the first release candidate PHP 7.1 recently came out, which means there will be no more significant changes and you can tell what exactly the changes will be in the release. I decided to revive a little dry “changelog” with my free translation of the changes that the new minor version of 7.x branches will bring us.

New functionality


Added “void” return type ( RFC )


Now functions and methods that should not return anything can be marked with a void return type:

function someNethod(): void { //   return  //   return; //    return null; //    return 123; } 

Returning a value from a method / function that is marked void will generate a Fatal Error level exception. Please note that NULL value is not equal to void (no value), that is, you cannot return NULL .

By the way, this does not mean that $ x = someNethod (); will not return anything. As before, $ x will be NULL . Also, void cannot be used as a type to a parameter.
')
 function bar(void $foo) {} // : Fatal error: void cannot be used as a parameter type in.... 

Added new pseudo-type: “iterable” ( RFC )


 function walkList(iterable $list): iterable { foreach ($list as $value) { yield $value['id']; } } 

This type essentially combines the primitive type of the array and the interface Traversable (and hence its derivatives: Iterator , Generator , etc). The problem arose from the fact that, for example, foreach can work with both types, but the function with the type array will not accept an object with the Traversable interface and vice versa.

Also, within the framework of this RFC, a new function, is_iterable () , has been added, which works similarly to other is_ * functions.

Now it is possible to resolve null in typed and returned parameters ( Nullable RFC )


 function callMethod(?Bar $bar): ?Bar {} $this->callMethod($bar); //  $this->callMethod(null); //  $this->callMethod(); //   

Note that the use of "?" and the default value of null is not the same

 function callMethod(int $bar = null) {} $this->callMethod(1); //  $this->callMethod(null); //  $this->callMethod(); //   

And the addition of "?" leaves the behavior backwards compatible

 function callMethod(?Bar $bar = null) {} //       “?” 

Also an important point to inherit:

 interface Fooable { function foo(int $i): ?Fooable; } interface StrictFooable extends Fooable { function foo(?int $i): Fooable; // valid } 

In the heir, you can make a “stricter” return type (that is, prohibit nullable ), and expand the parameter to the contrary to nullable , but not vice versa!

Added the ability to use a negative value for offset in rows ( RFC )


 echo $msg[-1]; //    echo $msg{-3}; //  RFC     $str{}   $str[]            . 

Negative values are also allowed in some steel string functions: strpos, stripos, substr_count, grapheme_strpos , grapheme_stripos, grapheme_extract, iconv_strpos, file_get_contents, mb_strimwidth, mb_ereg_search_setpos, mb_strpos, mb_stripos.

Everywhere this means counting the offset from the end of the line.

Allowed to use string keys in the list () ( RFC ) construction


A short syntax for list ( RFC ) has also been added.

 ["test" => $a, "name" => $b] = ["name" => "Hello", "test" => "World!"]; var_dump($a); // World! var_dump($b); // Hello 

Features:


Converting callable closure expressions ( RFC )


 Closure::fromCallable(callable $calback); 

Here is a clear example of application:

 class A { public function getValidator(string $name = 'byDefault') { return Closure::fromCallable([$this, $name]); } private function byDefault(...$options) { echo "Private default with:".print_r($options, true); } public function __call ( string $name , array $args ) { echo "Call $name with:".print_r($args, true); } } $a = new A(); $a->getValidator("test")(1,2,3); // Call test with: Array ( [0] => 1 [1] => 2 [2] => 3 ) $a->getValidator()('p1', 'p2'); // Private default with: Array ( [0] => 'p1', [1] => 'p2') //  Closure::fromCallable   ($this)     ,        //    return [$this, $name];  $a->getValidator()('p1', 'p2'); //  // Call byDefault with:Array ( [0] => p1 [1] => p2 ) //                

Visibility modifier support for class constants ( RFC )


 class Token { //      “public” const PUBLIC_CONST = 0; //      private const PRIVATE_CONST = 0; protected const PROTECTED_CONST = 0; public const PUBLIC_CONST_TWO = 0; //       private const FOO = 1, BAR = 2; } 

You can catch exceptions by combining several types of exceptions into one block ( RFC )


 try { echo "OK"; } catch (Exception | DomainException $e) { // ...  2    } catch (TypeError $e) { // ... } 

Emissions of E_NOTICE and E_WARNING level errors during arithmetic operations on strings containing non-valid numbers ( RFC )


 $numberOfApples = "10 apples" + "5 pears"; //  // Notice: A non well formed numeric string encountered in example.php on line 3 // Notice: A non well formed numeric string encountered in example.php on line 3 $numberOfPears = 5 * "orange"; // Warning: A non-numeric string encountered in example.php on line 3 

This is quite an important change that theoretically can break the backward compatibility of an application if its error handlers are used to intercept warnings.

And there is an interesting feature: the space at the beginning of the lines “5” + “3” will not give errors. But “5” + “3” - a space at the end will give warnings.

To bypass the consequences of implicit conversion and ejection of warnings, you can explicitly specify a “cast” to the desired type: (int) “5” + (int) “3” or force all to be suppressed @ (“5” + “3”).

Other changes and backward incompatibilities



On this, we probably dwell, although there are still a lot of small changes, mainly in extensions. And for us for holivar this list is quite enough. )

Total


Personally, my opinion about this minor release: everything fits very organically, this is precisely what was missing in the majority in the new PHP 7.0 and these changes only emphasize and strengthen the features of 7.x branches.
I recommend waiting for 7.1.1 and you can upgrade without fear, break something (unless of course you have already moved to 7.0).

This article does not pretend to a complete description of ALL changes and I could miss something important, I recommend to get acquainted with the original sources anyway:
» Https://wiki.php.net/rfc#php_71
» Https://github.com/php/php-src/blob/php-7.1.0RC1/UPGRADING

PS Examples can be experienced in the online sandbox - 3v4l.org/#version=7.1.0RC1

Source: https://habr.com/ru/post/309858/


All Articles