Home > headlines > body text

The exciting PHP7.4

藏色散人
Release: 2019-09-30 18:07:21
forward
10698 people have browsed it

The exciting PHP7.4

PHP 7.4 is the next minor version of PHP 7 and is expected to be released to General Availability on November 28, 2019. Let’s take a look at the new features in PHP 7.4 that will make PHP faster and more reliable.

Of course, I am more looking forward to PHP 8. Because some of JIT 's proposals have been approved, this may become another milestone for PHP.

What’s new in PHP using PHP 7.4?

● Support unpacking within arrays - array expansion operator

● Arrow function 2.0 (shorter closure)

PHP 7.4 in arrays The Spread operator

is introduced in expressions and is available since PHP 5.6. Parameter unpacking is the syntax for unpacking arrays and Traversable into parameter lists. To unpack an Array or Traversable, it must be prefixed with ... (3 dots), as in the following example:

  function test(...$args) { var_dump($args); }
  test(1, 2, 3);
Copy after login

HoweverPHP 7.4 RFC recommends extending this functionality to arrays Definition:

  $arr = [...$args];
Copy after login

The first benefit of the Spread operator is performance, RPC documentation states that :

The Spread operator should have better performance than array_merge. It's not just that the spread operator is a syntax construct and array_merge is a method. Also at compile time, constant arrays are optimized for high efficiency. A significant advantage of the Spread operator is that it supports any traversable object, whereas the array_merge function only supports arrays.

The following is an example of a parameter in an array with the Spread operator:

   $parts = ['apple', 'pear'];
  $fruits = ['banana', 'orange', ...$parts, 'watermelon'];
  var_dump($fruits);
Copy after login

If you run this code in PHP 7.3 or earlier, PHP will throw a Parse error:

Parse error: syntax error, unexpected '...' (T_ELLIPSIS), expecting ']' in /app/spread-operator.php on line 3
Copy after login

Instead, PHP 7.4 will return an array

array(5) {
    [0]=>
    string(6) "banana"
    [1]=>
    string(6) "orange"
    [2]=>
    string(5) "apple"
    [3]=>
    string(4) "pear"
    [4]=>
    string(10) "watermelon"
  }
Copy after login

The RFC states that we can extend the same array multiple times. Furthermore, we can use the Spread Operator syntax anywhere in the array since regular elements can be added before or after the spread operator. Therefore, the following code will work as expected:

$arr1 = [1, 2, 3];
  $arr2 = [4, 5, 6];
  $arr3 = [...$arr1, ...$arr2];
  $arr4 = [...$arr1, ...$arr3, 7, 8, 9];
Copy after login

It is also possible to pass the array returned by the function as a parameter and put it into a new array:

function buildArray(){
    return ['red', 'green', 'blue'];
  }
  $arr1 = [...buildArray(), 'pink', 'violet', 'yellow'];
Copy after login

PHP 7.4 outputs the following array:

array(6) {
    [0]=>
    string(3) "red"
    [1]=>
    string(5) "green"
    [2]=>
    string(4) "blue"
    [3]=>
    string(4) "pink"
    [4]=>
    string(6) "violet"
    [5]=>
    string(6) "yellow"
  }
Copy after login

We can also use

generator

:

but passing by reference is not allowed. Consider the following example:

$arr1 = ['red', 'green', 'blue'];
  $arr2 = [...&$arr1];
Copy after login

If we try to pass by reference, PHP will throw the following Parse error:

Parse error: syntax error, unexpected '&' in /app/spread-operator.php on line 3
Copy after login

If the elements of the first array are stored by reference, then They are also stored by reference in the second array. Here is an example:

   $arr0 = 'red';
  $arr1 = [&$arr0, 'green', 'blue'];
  $arr2 = ['white', ...$arr1, 'black'];
Copy after login

This is what we get with PHP 7.4:

array(5) {
    [0]=>
    string(5) "white"
    [1]=>
    &string(3) "red"
    [2]=>
    string(5) "green"
    [3]=>
    string(4) "blue"
    [4]=>
    string(5) "black"
  }
Copy after login

Arrow Functions 2.0 (Short Closure)

In PHP In , anonymous functions are considered to be very verbose and difficult to implement and maintain,

RFC

It is recommended to introduce a simpler and clearer arrow function (or short closure) syntax so that we can write code concisely . Before PHP 7.4:

function cube($n){
    return ($n * $n * $n);
  }
  $a = [1, 2, 3, 4, 5];
  $b = array_map('cube', $a);
  print_r($b);
Copy after login

PHP 7.4 allows for a more concise syntax, the above function can be rewritten as follows:

$a = [1, 2, 3, 4, 5];
  $b = array_map(fn($n) => $n * $n * $n, $a);
  print_r($b);
Copy after login

Currently, due to the language structure,

Anonymous function

(closure) can use use to inherit variables defined in the parent scope, as shown below:

But in PHP 7.4, the value of the parent scope is captured implicitly (implicitly bound by the scope of the value). So we can complete this function in one line:

$factor = 10;
  $calc = fn($num) => $num * $factor;
Copy after login

Variables defined in the parent scope can be used for arrow functions. It is equivalent to our use of use and cannot be modified by the parent.

The new syntax is a great improvement to the language because it allows us to build more readable and maintainable code.

NULL coalescing operator

Due to a large number of situations where ternary expressions and isset () are used simultaneously in daily use, we added the null coalescing operator (? ?) This syntactic sugar. If the variable exists and is not NULL, it returns its own value, otherwise it returns its second operand.

$username = $_GET['user'] ?? ‘nobody';
Copy after login

What this code does is very simple: it gets the request parameter and sets the default value if it doesn't exist. But in this RFC example, what if we have longer variable names?

$this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ?? 'value';
Copy after login

长远来看,这段代码可能难以维护。因此,旨在帮助开发人员编写更直观的代码,这个 RFC 建议引入 null 合并等于运算符 (null_coalesce_equal_operator)??=,所以我们可以敲下面这段代码来替代上面的这段代码:

$this->request->data['comments']['user_id'] ??= ‘value’;
Copy after login

如果左侧参数的值为 null,则使用右侧参数的值。

注意,虽然 coalesce 运算符 ?? 是一个比较运算符,但 ??= 它是赋值运算符。

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template