Five new features of PHP 7_PHP Tutorial

WBOY
Release: 2016-07-12 09:06:42
Original
870 people have browsed it

Five new features of PHP 7


1. Operator NULL merge operator)

I put this first because I find it useful. Usage:

$a = $_GET['a'] ?? 1;

It is equivalent to:

$a = isset($_GET['a']) ? $_GET['a'] : 1;

We know that the ternary operator can be used like this:

$a ?: 1

But this is based on the premise that $a has been defined. The new ?? operator can simplify judgment.

2. Function return value type declaration

The examples provided by the official documentation note that the side length parameter syntax of ... is only available in PHP 5.6 and above):

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
  1. <span><span><php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. function arraysSum(array ...$arrays): array
  3. {
  4.  return array_map(function(array $array): int {
  5.  return array_sum($array);
  6. }, $arrays);
  7. }
  8. print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));

It can be seen from this example that now functions (including anonymous functions) can specify the type of return value.

The way this statement is written is somewhat similar to swift:

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
  1. <span><span>func sayHello(personName: String) -> String { </span></span>
    Copy after login
  2. let greeting = "Hello, " personName "!"
  3. return greeting
  4. }

This feature can help us avoid some problems caused by PHP's implicit type conversion. Thinking about the expected results before defining a function can avoid unnecessary mistakes.

However, there is also a feature that needs attention here. PHP 7 adds a declare directive: strict_types, which uses strict mode.

When using return value type declaration, if it is not declared in strict mode, if the return value is not of the expected type, PHP will still cast it. But if it is strict mode, a Fatal error of TypeError will be triggered.

Forced mode:

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
  1. <span><span><php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. function foo($a) : int
  3. {
  4. return $a;
  5. }
  6. foo(1.0);

The above code executes normally and the foo function returns int 1 without any errors.

Strict mode:

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
  1. <span><span><php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. declare(strict_types=1);
  3. function foo($a) : int
  4. {
  5. return $a;
  6. }
  7. foo(1.0);
  8. # PHP Fatal error: Uncaught TypeError: Return value of foo() must be of the type integer, float returned in test.php:6

在声明之后,就会触发致命错误。

是不是有点类似与 js 的 strict mode?

3. 标量类型声明

PHP 7 中的函数的形参类型声明可以是标量了。在 PHP 5 中只能是类名、接口、array 或者 callable (PHP 5.4,即可以是函数,包括匿名函数),现在也可以使用 string、int、float和 bool 了。

官方示例:

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
  1. <span><span><php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. // Coercive mode
  3. function sumOfInts(int ...$ints)
  4. {
  5. return array_sum($ints);
  6. }
  7. var_dump(sumOfInts(2, '3', 4.1));

需要注意的是上文提到的严格模式的问题在这里同样适用:强制模式默认,既强制类型转换)下还是会对不符合预期的参数进行强制类型转换,严格模式下则触发 TypeError 的致命错误。

4. use 批量声明

PHP 7 中 use 可以在一句话中声明多个类或函数或 const 了:

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
  1. <span><span><php </span></span>
    Copy after login
    Copy after login
    Copy after login
    Copy after login
    Copy after login
  2. use some/namespace/{ClassA, ClassB, ClassC as C}; 
  3. use function some/namespace/{fn_a, fn_b, fn_c}; 
  4. use const some/namespace/{ConstA, ConstB, ConstC}; 
  5. 但还是要写出每个类或函数或 const 的名称并没有像 python 一样的 from some import * 的方法)。

    需要留意的问题是:如果你使用的是基于 composer 和 PSR-4 的框架,这种写法是否能成功的加载类文件?其实是可以的,composer 注册的自动加载方法是在类被调用的时候根据类的命名空间去查找位置,这种写法对其没有影响。

    5. 其他的特性

    其他的一些特性我就不一一介绍了,有兴趣可以查看官方文档:http://php.net/manual/en/migration70.new-features.php

    简要说几个:

    • PHP 5.3 开始有了匿名函数,现在又有了匿名类了;

    • define 现在可以定义常量数组;

    • 闭包 Closure)增加了一个 call 方法;

    • 生成器或者叫迭代器更合适)可以有一个最终返回值return),也可以通过 yield from 的新语法进入一个另外一个生成器中生成器委托)。

    生成器的两个新特性return 和 yield from)可以组合。具体的表象大家可以自行测试。PHP 7 现在已经到 RC5 了,最终的版本应该会很快到来。

    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1064062.htmlTechArticleFive new features of PHP 7 1. Operator NULL coalescing operator) Put this first and say yes Because I find it useful. Usage: $a = $_GET['a'] ?? 1; It is equivalent to: php $a = is...
Related labels:
source:php.cn
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