PHP8.0新功能之Match表达式的使用

吾爱主题 阅读:124 2021-10-20 13:20:00 评论:0

上个月下旬php社区发布是php8第一个,正式版本也将于今年年底发布。php8带来来那个两个最令人激动的特性:jit和match表达式。

本文我们要说另一个新引入的语法match表达式语法,可以说是php 8引入的最好的功能之一,它使用类似switch的语法。

基本功能

?
1 2 3 4 5 $status = match( $request_method ) { 'post' => $this ->handlepost(), 'get' , 'head' => $this ->handleget(), default => throw new \exception( 'unsupported' ), };

用switch...case做对比,要实现上面的功能,代码要略繁琐一点:

?
1 2 3 4 5 6 7 8 9 10 11 switch ( $request_method ) { case 'post' : $status = $this ->handlepost(); break ; case 'get' : case 'head' : $status = $this ->handleget(); break ; default : throw new \exception( 'unsupported' ); };

相比switch, match会直接返回值,无需中间变量(比如上例中的$status)。

表达式可以返回一个值

在每个分支可以被分配给一个变量。

?
1 2 3 4 $name = match(2) { 1 => 'one' , 2 => 'two' , };

不必再将返回值分配给其他中变量,匹配的语句返回值可直接从match表达式中返回。

可匹配多个条件

match表达式可能包含一个或多个匹配条件,它们的行为类似于块中的多个级联case键switch。

?
1 2 3 4 match( $request_method ) { 'post' => $this ->handlepost(), 'get' , 'head' => $this ->handleget(), };

满足$request_method === 'get'和$request_method === 'head'两个条件都会执行$this->handleget()。

每个分支只能包含一个表达式

与switch可以包含任意数量的表达式的块不同,一条match语句只能包含一个表达式。

?
1 2 3 4 5 match( $name ) { 'xxx' => init(); doth(); };

上面的语法错误的。=>只能有一个表达式。

隐含的break

match表达式的每个匹配分支仅允许一个表达式,并且无需switch块一样的break。

?
1 2 3 4 5 6 switch ( 'test' ) { case 'test' : $this ->dotest (); case 'send' : $this ->sendmsg (); }

在switch...caser容易犯的错误是忘记了break语句,这会使流程直接进入下一分支。在上面的switch块中,缺少break;语句会使代码$this->dotest()无法正常执行执行。

?
1 2 3 4 match ( 'test' ) { 'test' => $this ->dotest (), 'send' => $this ->sendmsg (), };

match表达式无需显式break语句即可工作。它只执行一个match分支,并立即返回该值。

default分支

match语句支持一个default分支,该分支工作原理与switch...case块中的default情况类似。如果没有其他条件相匹配,将执行default match分支。

?
1 2 3 4 5 6 7 match ( 'def' ) { 'aaa' => ..., 'bbb' => ..., default => echo 'no matching: ' . $name , };   // "no matchin: deff"

match表达式必须符合条件

switch如果没有匹配case键,则block静默进行代码流。match表达式没有。

在match表达式中,必须存在与表达式匹配的条件或default要处理的条件。如果没有匹配项,而且为设置default分支,match表达式将会引发\unhandledmatcherror异常。

?
1 2 3 4 5 $value = 3; match( $value ) { 1 => 'one' , 2 => 'two' , };

上面的代码执行时候会抛出错误:

fatal error: uncaught unhandledmatcherror in ... 

match\unhandledmatcherror如果表达式中没有匹配项,则表达式将引发异常。

\unhandledmatcherror是php 8中的新异常类,它扩展了\error。有关所有php核心异常类的完整层次结构。

该类可以很容易地扩展:

?
1 class unhandledmatcherror extends \error {}

对非强制类型的严格匹配

match表达式中最重要的设计选择之一是它对非强制类型的匹配。

?
1 2 3 4 5 6 7 8 9 10 11 12 function read(mixed $key ): string { return match ( $key ) { 1 => 'integer 1' , '1' => 'string 1' , true => 'bool true' , [] => 'empty array' , [1] => 'array [1]' , }; }   read(1); // "integer 1" read( '1' ); // "string 1"

在典型的switch块中,其大小写是松散匹配的,即与==。在match表达式中,所有匹配的分支都经过严格的比较(===)匹配。

在上面的代码段中,每个单独的分支都将匹配其值和类型。

匹配任意表达式

match 表达式允许给定值与表达式匹配。

?
1 2 3 4 5 6 7 match( $httpst ){ 404 => 'page not found' , response::redirect => 'redirect' , $client ->getcode() => 'client error' , $response ->getcode() => 'response error' , default => 'unknown error' };

表达式将按照其排列顺序进行求值。

match表达式将尝试$httpst按以下顺序进行匹配:

1. $httpst === 404
2. $httpst === response::redirect
3. $httpst === $client->getcode()
4. $httpst === $response->getcode()
5. default

如果找到正匹配,则将不会对其他分支进行尝试,直接返回。

match vs switch

向后兼容性影响

match表达式是php 8中的新语法。使用match表达式的代码在较旧的php版本中将不起作用。

到此这篇关于php8.0新功能之match表达式的使用的文章就介绍到这了,更多相关php8.0 match表达式内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://developer.51cto.com/art/202007/621293.htm

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容