PHP头条
热点:

PHP7.1和7.2 新增功能详解,php7.17.2新增功能


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

php7.1 新增功能

1.可为空(Nullable)类型

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

看下例子:

function testReturn(?string $name)
{
    return $name;
}

var_dump(testReturn('yangyi'));
var_dump(testReturn(null));
var_dump(testReturn2());

打印输出:



$ php php71.php

string(6) "yangyi"

NULL

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
Stack trace:
#0 php71.php(22): testReturn()
#1 {main}
  thrown in php71.php on line 14

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

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

function testReturn3() : ?string
{
    //return "abc";
    //return null;
}

var_dump(testReturn3());

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

2.void返回类型

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

function testReturn4() : void
{
    //1. 要么啥都不返还 ok

    //2. 要么只return; ok
    //return;

    //3. return null 也会报错
    //return null;

    //4. return 4 会报错
    //return 4;
}
Fatal error: A void function must not return a value in /php71.php on line 70

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

function testReturn6(void $a) : void
{

}

var_dump(testReturn6());
PHP Fatal error:  void cannot be used as a parameter type in php71.php on line 73

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

<?php 

class Foo
{
    public function bar(): void {
    }
}

class Foobar extends Foo
{
    // 覆盖失败
    public function bar(): array { 
        // Fatal error: Declaration of Foobar::bar() must be compatible with Foo::bar(): void
    }
}

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

class Foo
{
    public $a;
    public function bar(): void {
        $this->a = 2;
    }
}

class Foobar extends Foo
{
    // 覆盖成功
    public function bar(): void {
        $this->a = 3;
    }
}

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

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

$data = [
    [1, 'Tom'],
    [2, 'Fred'],
];

// list() style
list($id1, $name1) = $data[0];

// [] style
[$id1, $name1] = $data[0];

// list() style
foreach ($data as list($id, $name)) {
    // logic here with $id and $name
}

// [] style
foreach ($data as [$id, $name]) {
    // logic here with $id and $name
}

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

$data = [
    ["id" => 1, "name" => 'Tom'],
    ["id" => 2, "name" => 'Fred'],
];

// list() style
list("id" => $id1, "name" => $name1) = $data[0];

// [] style
["id" => $id1, "name" => $name1] = $data[0];

// list() style
foreach ($data as list("id" => $id, "name" => $name)) {
    // logic here with $id and $name
}

// [] style
foreach ($data as ["id" => $id, "name" => $name]) {
    // logic here with $id and $name
}

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

$data = [
    ["id" => 1, "name" => 'Tom'],
    ["id" => 2, "name" => 'Fred'],
];

while (list($id, name) = each($data)) {
    echo "$key => $val\n";
}

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

3.类常量可见范围设定

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

<?php 
class ConstDemo 
{
    // 常量默认为 public
    const PUBLIC_CONST = 0;

    // 可以自定义常量的可见范围
    public const PUBLIC_CONST_B = 2;
    protected const PROTECTED_CONST = 3;
    private const PRIVATE_CONST = 4;

    // 多个常量同时声明只能有一个属性
    private const FOO = 1, BAR = 2;
}

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

4.支持负的字符串偏移

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

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

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

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

5.多条件 catch

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

try {
    // Some code...
} catch (ExceptionType1 $e) {
    // 处理 ExceptionType1
} catch (ExceptionType2 $e) {
    // 处理 ExceptionType2
} catch (Exception $e) {
    // ...
}

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

try {
    // Some code...
} catch (ExceptionType1 | ExceptionType2 $e) {
    // 对于 ExceptionType1 和 ExceptionType2 的处理
} catch (Exception $e) {
    // ...
}

php7.2

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

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

www.phpzy.comtrue/php/19899.htmlTechArticlePHP7.1和7.2 新增功能详解,php7.17.2新增功能 之前写过php7.0以及老版本的php各大版本的跟新点以及新功能。今天看下php7.1和php7.2的新功能。 php7.1 新增功能 1.可为空(Nullable)类型 参数和返...

相关文章

    暂无相关文章

PHP之友评论

今天推荐