本文主要是介绍PHP 8.0.0 Released!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
2020年11月26日,PHP开发团队宣布将发布PHP 8.0.0。此版本标志着PHP语言最新的主要版本。
PHP 8.0具有许多改进和新功能,例如:
1、Union Types ,联合类型
PHP 7
class Number {/** @var int|float */private $number;/*** @param float|int $number*/public function __construct($number) {$this->number = $number;}
}new Number('NaN'); // Ok
PHP 8
class Number {public function __construct(private int|float $number) {}
}new Number('NaN'); // TypeError
2、Named Arguments,命名参数
仅指定必需的参数,跳过可选的参数。
参数是与顺序无关的且具有自记录功能。
PHP 7
htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);
PHP 8
htmlspecialchars($string, double_encode: false);
3、Match Expressions,匹配表达式
新的匹配类似于switch,并具有以下功能:
Match是一个表达式,表示其结果可以存储在变量中或返回。
Match分支仅支持单行表达式,不需要中断。声明。
匹配进行严格的比较
。
PHP 7
switch (8.0) {case '8.0':$result = "Oh no!";break;case 8.0:$result = "This is what I expected";break;
}
echo $result;
//> Oh no!
PHP 8
echo match (8.0) {'8.0' => "Oh no!",8.0 => "This is what I expected",
};
//> This is what I expected
4、Attributes,注解
PHP 7
class PostsController
{/*** @Route("/api/posts/{id}", methods={"GET"})*/public function get($id) { /* ... */ }
}
PHP 8
class PostsController
{#[Route("/api/posts/{id}", methods: ["GET"])]public function get($id) { /* ... */ }
}
5、Constructor Property Promotion,构造函数属性提升
PHP 7
class Point {public float $x;public float $y;public float $z;public function __construct(float $x = 0.0,float $y = 0.0,float $z = 0.0,) {$this->x = $x;$this->y = $y;$this->z = $z;}
}
PHP 8
class Point {public function __construct(public float $x = 0.0,public float $y = 0.0,public float $z = 0.0,) {}
}
6、Nullsafe Operator,Nullsafe 运算符
现在,您可以使用带有新的nullsafe运算符的调用链来代替空检查条件。当对链中一个元素的求值失败时,整个链的执行将中止,并且整个链的求值为空。
PHP 7
$country = null;if ($session !== null) {$user = $session->user;if ($user !== null) {$address = $user->getAddress();if ($address !== null) {$country = $address->country;}}
}
PHP 8
$country = $session?->user?->getAddress()?->country;
7、WeakMap,弱映射
WeakMaps 允许创建从对象到任意值的映射(类似 SplObjectStorage),而不会阻止对用作 key 的对象进行垃圾回收。如果对象key是垃圾回收的,则将其简单地从WeakMap中删除。
PHP 7.4 中,已经引入了对 WeakReference(弱引用)的支持。但是,原始的弱引用本身用途有限,而 WeakMaps 在实践中更为常用。由于没有提供注册销毁回调的功能,因此无法在 PHP 弱引用之上实现有效的弱映射。
弱映射的一般用例是将数据与单个对象实例相关联,而不会强迫它们保持活动状态,避免长时间运行的进程无谓地占用内存。
class FooBar {private WeakMap $cache;public function getSomethingWithCaching(object $obj) {return $this->cache[$obj] ??= $this->computeSomethingExpensive($obj);}// ...
}
8、JIT,Just-In-Time ,即时编译
PHP 8引入了两个JIT编译引擎。Tracing JIT是两者中最有希望的,它在综合基准测试中的性能提高了大约3倍,在某些特定的长期运行的应用程序中提高了1.5–2倍。典型的应用程序性能与PHP 7.4相当。
9、字符串与数字的比较
PHP 8使用数字与字符串进行比较时,它将数字转换为字符串并使用字符串比较。
PHP 7
0 == 'foobar' // true
PHP 8
0 == 'foobar' // false
10、其他:
(1)、对算术/按位运算符进行更严格的类型检查;
var_dump([] % [42]);
// int(0)
// WTF?
(2)、static 返回类型
在PHP中,static的特殊类名称指的是实际调用方法的类,即使该方法是继承的。这称为“后期静态绑定”(LSB)。使它static可用作返回类型(除了self和parent类型之外)。
class Test {public function createFromWhatever($whatever): static {return new static($whatever);}
}
在这里,我们要指定XXX::createFromWhatever()将始终创建的实例XXX,而不是某些父类的实例。
另一个是withXXX()用于更改不可变对象的样式接口:
class Test {public function withWhatever($whatever): static {$clone = clone $this;$clone->whatever = $whatever;return $clone;}
}
(3)、混合类型
// Valid exampleclass A
{public function foo(int $value) {}
}class B extends A
{// Parameter type was widened from int to mixed, this is allowedpublic function foo(mixed $value) {}
}
(4)、一些新增函数
① str_starts_with
② str_ends_with
③ str_contains
str_starts_with ( string $haystack , string $needle ) : bool
str_ends_with ( string $haystack , string $needle ) : bool
str_contains ( string $haystack , string $needle ) : boolstr_starts_with 检查一个字符串是否以另一个字符串开头并是否返回布尔值(true/ false)。
str_ends_with 检查一个字符串是否以另一个字符串结尾,是否返回布尔值(true/ false)。str_contains检查另一个字符串中是否包含一个字符串,并返回一个布尔值(true/ false)是否找到该字符串。
END
如有问题请在下方留言。
或关注我的公众号“孙三苗”,输入“联系方式”。获得进一步帮助。
这篇关于PHP 8.0.0 Released!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!