我是靠谱客的博主 寒冷康乃馨,这篇文章主要介绍从未停止前进,PHP8.1带来了8个重要的新转变!,现在分享给大家,希望可以做个参考。

“php是世界上最好的语言”,它从未停止前进的步伐!PHP团队目前已经发布了PHP 8.1.0 RC 5版,而下一个版本将是第六个也是最后一个候选版本 (RC 6),将于近期发布。下面就给大家介绍一下在PHP8.1中会有哪8个重要的新转变,先一睹为快吧!

1、枚举(Enums)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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)

复制代码
1
2
3
4
5
6
7
8
9
10
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、初始化程序中的新内容(New in initializers)

复制代码
1
2
3
4
5
6
7
class PostStateMachine { public function __construct( private State $state = new Draft(), ) { } }
登录后复制

4、纤维,又名“绿线”(Fibers, a.k.a. "green threads")

复制代码
1
2
3
4
5
6
7
8
9
$fiber = new Fiber(function (): void { $valueAfterResuming = Fiber::suspend('after suspending'); // … }); $valueAfterSuspending = $fiber->start(); $fiber->resume('after resuming');
登录后复制

5、数组解包也支持字符串键(Array unpacking also supports string keys)

复制代码
1
2
3
4
$array1 = ["a" => 1]; $array2 = ["b" => 2]; $array = ["a" => 0, ...$array1, ...$array2]; var_dump($array); // ["a" => 1, "b" => 2]
登录后复制

6、一种可调用类(First class callables)

复制代码
1
2
3
function foo(int $a, int $b) { /* … */ } $foo = foo(...); $foo(a: 1, b: 2);
登录后复制

7、纯交集类型(Pure intersection types)

复制代码
1
2
3
function generateSlug(HasTitle&HasId $post) { return strtolower($post->getTitle()) . $post->getId(); }
登录后复制

8、新array_is_list功能(The new array_is_list function)

复制代码
1
2
3
4
5
6
$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
登录后复制

最后

以上就是寒冷康乃馨最近收集整理的关于从未停止前进,PHP8.1带来了8个重要的新转变!的全部内容,更多相关从未停止前进,PHP8.1带来了8个重要内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(178)

评论列表共有 0 条评论

立即
投稿
返回
顶部