26

PHP 8.0新功能:Match表达式

 3 years ago
source link: http://developer.51cto.com/art/202007/621293.htm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

e6ZBB37.jpg!web

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

621c814979f4b596a421a073fdda55b2.jpeg

基本功能

$status = match($request_method) { 
'post' => $this->handlePost(), 
'get', 'head' => $this->handleGet(), 
default => throw new \Exception('Unsupported'), 
}; 

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

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)。

表达式可以返回一个值

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

$name = match(2) { 
1 => 'One', 
2 => 'Two', 
}; 

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

可匹配多个条件

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

match($request_method) { 
'post' => $this->handlePost(), 
'get', 'head' => $this->handleGet(), 
}; 

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

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

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

match($name) { 
'XXX' => 
init(); 
doth(); 
}; 

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

隐含的break

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

switch ('test') {  
case 'test': 
$this->doTest ();  
case 'send':  
$this->sendmsg ();  
} 

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

match ('test') { 
'test' => $this->doTest (), 
'send' => $this->sendmsg (), 
}; 

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

default分支

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

match ('DEF') { 
'aaa' => ..., 
'bbb' => ..., 
default => echo 'NO matching: ' . $name, 
}; 
 
// "NO matchin: DEFF" 

match表达式必须符合条件

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

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

$value = 3; 
match($value) { 
1 => 'One', 
2 => 'Two', 
}; 

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

Fatal error: Uncaught UnhandledMatchError in ... 

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

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

该类可以很容易地扩展:

class UnhandledMatchError extends \Error {} 

对非强制类型的严格匹配

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

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 表达式允许给定值与表达式匹配。

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

QzYNn23.jpg!web

向后兼容性影响

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK