[关闭]
@phper 2018-01-23T03:23:05.000000Z 字数 3378 阅读 5397

PHP7.1和7.2 新增功能详解

php


之前写过php7.0以及老版本的php各大版本的跟新点以及新功能。今天看下php7.1和php7.2的新功能。

php7.1 新增功能

1.可为空(Nullable)类型

参数和返回值的类型声明可以通过在类型名称前添加一个问号(?)来标记为空(null)。表明函数参数或者返回值的类型要么为指定类型,要么为 null。

看下例子:

  1. function testReturn(?string $name)
  2. {
  3. return $name;
  4. }
  5. var_dump(testReturn('yangyi'));
  6. var_dump(testReturn(null));
  7. var_dump(testReturn2());

打印输出:

  1. $ php php71.php
  2. string(6) "yangyi"
  3. NULL
  4. PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function testReturn(), 0 passed in php71.php on line 22 and exactly 1 expected in php71.php:14
  5. Stack trace:
  6. #0 php71.php(22): testReturn()
  7. #1 {main}
  8. thrown in php71.php on line 14

如上如:第三个报了一个致命的错误。

再来看下,函数返回值是Nullable的情况:

  1. function testReturn3() : ?string
  2. {
  3. //return "abc";
  4. //return null;
  5. }
  6. var_dump(testReturn3());

如果加了? 要么返回 string ,要么返回null。不能啥也不返还。会报错。

2.void返回类型

PHP7.0 添加了指定函数返回类型的特性,但是返回类型却不能指定为 void,7.1 的这个特性算是一个补充。定义返回类型为 void 的函数不能有返回值,即使返回 null 也不行:

  1. function testReturn4() : void
  2. {
  3. //1. 要么啥都不返还 ok
  4. //2. 要么只return; ok
  5. //return;
  6. //3. return null 也会报错
  7. //return null;
  8. //4. return 4 会报错
  9. //return 4;
  10. }
  1. Fatal error: A void function must not return a value in /php71.php on line 70

还有就是,void 只能用于返回值,不能用于参数中。比如下面的会报错:

  1. function testReturn6(void $a) : void
  2. {
  3. }
  4. var_dump(testReturn6());
  1. PHP Fatal error: void cannot be used as a parameter type in php71.php on line 73

如果在类的继承中,申明为void返回类型的方法,子类要是继承重写,也只能返回void, 否则会触发错误:

  1. <?php
  2. class Foo
  3. {
  4. public function bar(): void {
  5. }
  6. }
  7. class Foobar extends Foo
  8. {
  9. // 覆盖失败
  10. public function bar(): array {
  11. // Fatal error: Declaration of Foobar::bar() must be compatible with Foo::bar(): void
  12. }
  13. }

所以,你必须这样,就不会报错:

  1. class Foo
  2. {
  3. public $a;
  4. public function bar(): void {
  5. $this->a = 2;
  6. }
  7. }
  8. class Foobar extends Foo
  9. {
  10. // 覆盖成功
  11. public function bar(): void {
  12. $this->a = 3;
  13. }
  14. }

3.list 的方括号([])简写以及增加指定key

可以用list 来快速遍历得到数组里面的值。现在可以用[]简写了。

  1. $data = [
  2. [1, 'Tom'],
  3. [2, 'Fred'],
  4. ];
  5. // list() style
  6. list($id1, $name1) = $data[0];
  7. // [] style
  8. [$id1, $name1] = $data[0];
  9. // list() style
  10. foreach ($data as list($id, $name)) {
  11. // logic here with $id and $name
  12. }
  13. // [] style
  14. foreach ($data as [$id, $name]) {
  15. // logic here with $id and $name
  16. }

此外,此次更新的list,针对索引数组,还可以指定 key,这个升级非常棒,非常方便。

  1. $data = [
  2. ["id" => 1, "name" => 'Tom'],
  3. ["id" => 2, "name" => 'Fred'],
  4. ];
  5. // list() style
  6. list("id" => $id1, "name" => $name1) = $data[0];
  7. // [] style
  8. ["id" => $id1, "name" => $name1] = $data[0];
  9. // list() style
  10. foreach ($data as list("id" => $id, "name" => $name)) {
  11. // logic here with $id and $name
  12. }
  13. // [] style
  14. foreach ($data as ["id" => $id, "name" => $name]) {
  15. // logic here with $id and $name
  16. }

在这个功能没有之前,我们一般会用while + each 的方式来用list 遍历索引数组:

  1. $data = [
  2. ["id" => 1, "name" => 'Tom'],
  3. ["id" => 2, "name" => 'Fred'],
  4. ];
  5. while (list($id, name) = each($data)) {
  6. echo "$key => $val\n";
  7. }

注意:PHP 7.2 中已经将 each 函数移除了!所以,就不要用这种方式来遍历索引数组了

3.类常量可见范围设定

之前类里面额常量用const申明,是没有可见属性的。现在把方法的可见属性移植了过来:

  1. <?php
  2. class ConstDemo
  3. {
  4. // 常量默认为 public
  5. const PUBLIC_CONST = 0;
  6. // 可以自定义常量的可见范围
  7. public const PUBLIC_CONST_B = 2;
  8. protected const PROTECTED_CONST = 3;
  9. private const PRIVATE_CONST = 4;
  10. // 多个常量同时声明只能有一个属性
  11. private const FOO = 1, BAR = 2;
  12. }

使用方法和类的方法一样。就不多详述了。

4.支持负的字符串偏移

有2个更新,1是字符串直接取,2是strpos函数第三个参数支持负数。表示从尾部取。

  1. var_dump("abcdef"[-2]); // e
  2. var_dump(strpos("aabbcc", "b", -3)); //3

string变量可以直接取值,不用通过变量名,是在php5.5加入的。现在可以从尾部取:

  1. var_dump("abcdef"[-2]); // 从末尾取倒数第2个字符:e
  2. var_dump("abcdef"[2]); // 从前面取第2个,从0开始:c
  3. $string = 'bar';
  4. echo $string[1], $string[-1]; // a r

5.多条件 catch

在以往的 try ... catch 语句中,每个 catch 只能设定一个条件判断:

  1. try {
  2. // Some code...
  3. } catch (ExceptionType1 $e) {
  4. // 处理 ExceptionType1
  5. } catch (ExceptionType2 $e) {
  6. // 处理 ExceptionType2
  7. } catch (Exception $e) {
  8. // ...
  9. }

现在可以多个一起处理。用"|" 分割。

  1. try {
  2. // Some code...
  3. } catch (ExceptionType1 | ExceptionType2 $e) {
  4. // 对于 ExceptionType1 和 ExceptionType2 的处理
  5. } catch (Exception $e) {
  6. // ...
  7. }

php7.2

php 7.2大都是底层的更新,提高性能。没有太大常用语法层面的更新,这里就略过了。

参考文档:
http://php.net/manual/zh/migration71.php
http://www.woola.net/detail/2016-10-11-php-71-feature.html

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注