“php是世界上最好的語(yǔ)言”,它從未停止前進(jìn)的步伐!PHP團(tuán)隊(duì)目前已經(jīng)發(fā)布了PHP 8.1.0 RC 5版,而下一個(gè)版本將是第六個(gè)也是最后一個(gè)候選版本 (RC 6),將于近期發(fā)布。下面就給大家介紹一下在PHP8.1中會(huì)有哪8個(gè)重要的新轉(zhuǎn)變,先一睹為快吧!
1、枚舉(Enums)
enum Status { case draft; case published; case archived; public function color(): string { return match($this) { Status::draft => 'grey', Status::published => 'green', Status::archived => 'red', }; } }
2、只讀屬性(Readonly properties)
class PostData { public function __construct( public readonly string $title, public readonly string $author, public readonly string $body, public readonly DateTimeImmutable $createdAt, public readonly PostState $state, ) {} }
3、初始化程序中的新內(nèi)容(New in initializers)
class PostStateMachine { public function __construct( private State $state = new Draft(), ) { } }
4、纖維,又名“綠線”(Fibers, a.k.a. "green threads")
$fiber = new Fiber(function (): void { $valueAfterResuming = Fiber::suspend('after suspending'); // … }); $valueAfterSuspending = $fiber->start(); $fiber->resume('after resuming');
5、數(shù)組解包也支持字符串鍵(Array unpacking also supports string keys)
$array1 = ["a" => 1]; $array2 = ["b" => 2]; $array = ["a" => 0, ...$array1, ...$array2]; var_dump($array); // ["a" => 1, "b" => 2]
6、一種可調(diào)用類(First class callables)
function foo(int $a, int $b) { /* … */ } $foo = foo(...); $foo(a: 1, b: 2);
7、純交集類型(Pure intersection types)
function generateSlug(HasTitle&HasId $post) { return strtolower($post->getTitle()) . $post->getId(); }
8、新array_is_list功能(The new array_is_list function)
$list = ["a", "b", "c"]; array_is_list($list); // true $notAList = [1 => "a", 2 => "b", 3 => "c"]; array_is_list($notAList); // false $alsoNotAList = ["a" => "a", "b" => "b", "c" => "c"]; array_is_list($alsoNotAList); // false
本文系翻譯,原文地址:https://stitcher.io/blog/php-81-in-8-code-blocks
聲明:本文原創(chuàng)發(fā)布php中文網(wǎng),轉(zhuǎn)載請(qǐng)注明出處,感謝您的尊重!如有疑問(wèn),請(qǐng)聯(lián)系admin@php.cn處理
- 上一篇:就憑這三招,Vue 讓其它框架俯首稱臣!
- 下一篇:沒(méi)有了