📜 ⬆️ ⬇️

PHP 7.1: Upcoming Changes



Below are the major changes introduced by PHP 7.1 . For a complete list of currently approved and discussed changes, check out the official PHP RFC .


Interception of several types of exceptions at once


In some cases, we handle various exceptions equally and there is no possibility to inherit them from a common ancestor, which leads to code duplication. For example:
')
 try { // to do something } catch (MyException $e) { // Handle this exception } catch (AnotherException $e) { // Handle this in the same way as MyException } catch (Exception $e) { // Handle this in a different way } 

In PHP 7.1, both exceptions, handled in the example in the same way, can be caught simultaneously:

 try { // to do something } catch (MyException | AnotherException $e) { // Handle these exceptions } catch (Exception $e) { // Handle this in a different way } 

Pay attention to the syntax - it is not normal || the operator we associate with or is a single character | .

Curl HTTP/2 server push support


First, what is server push ? The best way may be to understand this through an example.

As you know, when a user makes a request to domain.com (do not take into account DNS, etc.), the web server responds with a bunch of markup, which the browser then interprets and displays. As part of this interpretation, performed by the browser, it needs to understand what additional resources it needs to request in order to fully display the page. This includes CSS, JavaScript files and images.

Server push aims to speed up boot time and allows you to skip this step, immediately directing resources to the client directly.

This functionality is available in libcurl from version 7.44.0 , but is still unstable and has not gone into release (Details: here and here ). For more information, read the official PHP RFC: ext / curl HTTP / 2 Server Push Support .

Scopes for class constants


Class constants cannot currently be made private or protected . They are always public .

For example:

 class Panda { private $pandaId; public $name; // This is public, and there's // nothing you can do about it. const MAGIC_POWER = 20; } 

PHP 7.1 introduces visibility modifiers for constants:

 class Panda { // Constants default to public const MAGIC_POWER = 20; private const LIMIT_BREAK = 30; protected const EXPERIENCE_POINTS = 0; public const HEALTH_POINTS = 100; } 

This puts constants on par with other members of the class.

Return type void


Return types were added in PHP 7.0 . Remember, this allows the developer to explicitly declare the type of the return value of the function.

For example:
 function foo(): array { return []; } 

Starting with PHP 7.1, you can specify that the function is of type void , i.e. it performs the action, but returns nothing.

 function i_dont_return_anything(): void { // Perform some action // This is perfectly valid. return; // This would throw a Fatal error (A // void function must not return a // value). // return true; } 

Of course, you can argue that a function should always return something, at least boolean , indicating successful execution, but this is a topic for another article.

Uniform behavior of string functions


Take a look at the PHP strrpos () function.

 strrpos($needle, $haystack, $offset = 0); 

The last parameter supports a negative offset, which instead of finding the last occurrence of $needle in $haystack starting from the beginning of the line, finds the last entry starting from N positions back from the end of $ haystack.

Many PHP-related string functions have this optional $offset parameter, and some do not. One of the clearest examples is strpos() . Here, in order to achieve a negative offset, the function must be combined with substr() , which reduces readability and code performance.

The following PHP update removes this space in the language by adding the $offset parameter and its behavior with a negative value to many standard functions.

Support for string parameters in the list() function and the new syntax c []


Currently PHP has the ability to convert an array to a list of variables using the list() function:

Example :

 $myArray = ['monkey', 'tree', 'banana']; list($monkey, $tree, $banana) = $myArray; // $monkey is now equal to 'monkey'. 

_list() works only with numeric indices of arrays starting from zero, for example, as in the code above. It does not work with associative arrays, such as:

 $myNamedArray = [ 'name' => 'Amo', 'age' => 32, 'location' => 'London' ]; 

PHP 7.1 solves this:

 list('name' => $name, 'age' => $age, 'location' => $location) = $myNamedArray; 

Another significant improvement in PHP 7.1. is the introduction of square brackets [] to denote an array of variables. This provides an alternative to list() for dividing an array into variables.

 [$a, $b, $c] = [1, 2, 3]; 

Note that the same syntax is used as when creating the array:

 $numbers = [1, 2, 3]; 

Full details of this change are available on the RFC page.

Throw warning when invalid lines in arithmetic


When performing arithmetic in PHP, the language will be correct (or not, depending on how you look at it) mixes integer values ​​with string values ​​based on the numbers they contain.

Take the following examples:

 //Results in 10 $total = 5 + 5; // Results in 10 $total = '5' + '5'; // Results in 10 $total = 5+ '5'; // Results in 10 $total = '3 bananas yesterday' + '7 bananas today'; // Results in 5 $total = 5 + 'I love bananas'; 

The fourth example contains numeric values, and since the rest is cut out, then the sum of the remaining symbols used for the calculation will give a total of 10. But it would still be nice to see a warning about working with lines that are not intended for such behavior.

The last example also demonstrates this behavior in work. The line I love bananas no numeric values ​​and the whole line is treated as 0.

In PHP 7.1, examples four and five will return:

 Notice: A non well formed numeric string encountered in file.php on line x 

This introduces some problems with backward compatibility in cases where silent processing of such strings is expected. You can pre-convert the string to an integer through (int) "string" , but I personally think that if you perform arithmetic operations on such strings, then you should think about the quality of this code.

Outdated mcrypt() and its subsequent deletion


The mcrypt library was abandoned in 2007 and contains numerous bugs and unmortgaged patches. Thus, the question of its inclusion in PHP has long been ripe for consideration.

In PHP 7.1, all mcrypt_* functions will issue an E_DEPRECATED notification. In PHP 7.1 + 1, they will be completely removed.

Remember, if you use mcrypt in your PHP code, you do it wrong .

That's all for now, but if you come across any other core changes I didn't mention, please let us know in the comments below. Thanks for reading.

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


All Articles