Home  >  Article  >  The exciting PHP7.4

The exciting PHP7.4

藏色散人
藏色散人forward
2019-08-28 14:20:0510556browse

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);

HoweverPHP 7.4 RFC recommends extending this functionality to arrays Definition:

  $arr = [...$args];

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);

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

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"
  }

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];

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'];

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"
  }

We can also use

generator

:

function generator() {
    for ($i = 3; $i <= 5; $i++) {
        yield $i;
    }
  }
  $arr1 = [0, 1, 2, ...generator()];
but passing by reference is not allowed. Consider the following example:

$arr1 = [&#39;red&#39;, &#39;green&#39;, &#39;blue&#39;];
  $arr2 = [...&$arr1];

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

Parse error: syntax error, unexpected &#39;&&#39; in /app/spread-operator.php on line 3

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 = &#39;red&#39;;
  $arr1 = [&$arr0, &#39;green&#39;, &#39;blue&#39;];
  $arr2 = [&#39;white&#39;, ...$arr1, &#39;black&#39;];

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"
  }

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(&#39;cube&#39;, $a);
  print_r($b);

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);

Currently, due to the language structure,

Anonymous function

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

$factor = 10;
  $calc = function($num) use($factor){
    return $num * $factor;
  };
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;

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[&#39;user&#39;] ?? ‘nobody&#39;;

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[&#39;comments&#39;][&#39;user_id&#39;] = $this->request->data[&#39;comments&#39;][&#39;user_id&#39;] ?? &#39;value&#39;;

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

$this->request->data[&#39;comments&#39;][&#39;user_id&#39;] ??= ‘value’;

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

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

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete