php7.1函数,PHP7.1常用新特性和函数

在php7发布了之后,官方又紧接着发布了php7.1、php7.2,我们也接着使用了新版本。接下来就说说7.1有那些新的特性。

Nullable types

在7的时候,如果使用了强类型就必须传递或者返回指定的类型。现在新增了Nullable Types就可以传递或者返回null类型。像下面的那样:

function testNullable(string $argument): string

{

return $argument;

}

echo testNullable('test null'); // 输出 'test null'

var_dump(testNullable(null)); // 报错

上面示例如果是跑在php7上面会报以下语法错误,他是不支持传递一个null给string。

Fatal error: Uncaught TypeError: Argument 1 passed to testNullable() must be of the type string, null given.

下面的示例我们将采用新特性来保证代码不会出错,只需要在类型前面加上?号就可以支持null值。

function testNullable(?string $argument): ?string

{

return $argument;

}

echo testNullable('test null'); // 输出 'test null'

var_dump(testNullable(null)); // 输出 NULL

Void 返回值

返回值声明为void类型的方法要么干脆省去return语句,要么使用一个空的return语句。 对于void函数来说,NULL不是一个合法的返回值。

function getVoid(): void

{

return null;

}

getVoid();

上述代码会产生一个运行时错误。

Fatal error: A void function must not return a value (did you mean "return;" instead of "return null;"

改成下面的形式。

function getVoid(): void

{

return ; // 或者return 语句也不写

}

getVoid();

类常量可见性

在以前类的常量一直都是public可见性。现在支持protected,private。

class Test

{

const TEST_MAX = 2;

public const TEST_MIN = 0;

protected const TEST_MIDDLE = 1;

private const TEST_PRIVATE = 3;

}

iterable伪类

这可以被用在参数或者返回值类型中,它代表接受数组或者实现Traversable接口的对象。 至于子类,当用作参数时,子类可以收紧父类的iterable类型到array或一个实现了Traversable的对象。对于返回值,子类可以拓宽父类的array或对象返回值类型到iterable。

function iterator(iterable $iter)

{

foreach ($iter as $val) {

//

}

}

多异常捕获处理

一个catch语句块现在可以通过管道字符(|)来实现多个异常的捕获。 这对于需要同时处理来自不同类的不同异常时很有用。

在7.1以前我们需要这样来捕获多个异常.

try {

// some code

} catch (FirstException $e) {

// handle first

} catch (SecondException $e) {

} catch (ThreeException $e) {

}

7.1以后我们就可以像这样定义。

try {

// some code

} catch (FirstException | SecondException $e | ThreeException $e) {

// handle first and second exceptions

}

list键名

现在list()和新的[]语法支持在它内部去指定键名。这意味着它可以将任意类型的数组都赋值给一些变量(与短数组语法类似)。

$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

}

Closure::fromCallable

用于将callable快速地转为一个Closure对象。

class Test

{

public function exposeFunction()

{

return Closure::fromCallable([$this, 'privateFunction']);

}

private function privateFunction($param)

{

var_dump($param);

}

}

$privFunc = (new Test)->exposeFunction();

$privFunc('some value'); // 输出 string(10) "some value "

上面已经介绍重要的一些php7.1更新功能。如果你需要升级迁移你可以查看官方文档

。如果你需要查看php7的更新特性,你可以通过阅读这篇文章。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值