最人性特性:命名参数、联合类型、mixed类型
这几个新特性让PHP在强类型方面进一步完善,而且对PHPDoc的注释依赖越来越弱,代码即文档的好处是开发者最头疼的事情终于有办法可以偷懒了。
命名参数
命名参数可以让函数或者方法的调用更加清晰直观,对于如下的函数定义
function foo ( string $a , string $b , ? string $c = null , ? string $d = null )
{ }
你可以通过下面的方式传入参数进行调用
foo (
b: 'value b' ,
a: 'value a' ,
d: 'value d' ,
) ;
最大的好处是传入参数的顺序是和定义无关的,而且还可以混合传参(但不建议)。
联合类型
相对于以前的 PHPDoc 声明类型的组合, 现在可以用原生支持的联合类型声明取而代之,可在实际运行中验证。
PHP7
class Number {
private $number ;
public function __construct ( $number ) {
$this - > number = $number ;
}
}
new Number ( 'NaN' ) ;
PHP8
class Number {
public function __construct (
private int| float $number
) { }
}
new Number ( 'NaN' ) ;
新的 mixed类型
mixed本身是以下类型之一:
array
bool
callable
int
float
null
object
resource
string
注意,mixed也可以用作参数或属性类型,而不仅仅是返回类型。
另外由于mixed已经包含null,因此不允许将其设置为nullable。以下内容将触发错误:
function bar ( ) : ? mixed { }
最具贡献的特性:JIT
JIT作为PHP底层编译引擎,对于PHP8的性能贡献是非常之大,不过对于常规WEB应用来说,优势不明显,但仍然是非常的高大上特性,是PHP8的扛鼎之作。
PHP 8 引入了两个即时编译引擎。Tracing JIT 在两个中更有潜力,它在综合基准测试中显示了三倍的性能,
并在某些长时间运行的程序中显示了 1.5-2 倍的性能改进。典型的应用性能则和 PHP 7.4 不相上下。
最实用的特性:构造器属性提升、Nullsafe运算符、str_contains()、 str_starts_with()、 str_ends_with()
构造器属性提升
这个新的语法糖来用来创建值对象或数据传输对象。不用为类属性和构造函数指定它们,PHP 现在可以将它们合并为一个。
代替如下代码:
class Money
{
public Currency $currency ;
public int $amount ;
public function __construct (
Currency $currency ,
int $amount ,
) {
$this - > currency = $currency ;
$this - > amount = $amount ;
}
}
你可以这样做:
class Money
{
public function __construct (
public Currency $currency ,
public int $amount ,
) { }
}
nullsafe运算符
现在可以用新的 nullsafe 运算符链式调用,而不需要条件检查 null。如果链条中的一个元素失败了,整个链条会中止并认定为 Null。
$country = null ;
if ( $session !== null ) {
$user = $session - > user ;
if ( $user !== null ) {
$address = $user - > getAddress ( ) ;
if ( $address !== null ) {
$country = $address - > country ;
}
}
}
简化为一行代码
$country = $session ? - > user ? - > getAddress ( ) ? - > country ;
str_contains()、str_starts_with()和str_ends_with()函数
有些人可能会说它早就该有了,但我们终于不必再依赖strpos() 来知道字符串是否包含另一个字符串了。
代替如下:
if ( strpos ( '最讨厌红色的衣服了' , '红色' ) !== false ) { }
你可以这样做
if ( str_contains ( '最讨厌红色的衣服了' , '红色' ) ) { }
感觉大多数场景应该是不需要使用strpos了吧,外两个早就应该有了,str_starts_with()和str_ends_with()这两个函数现在能省事不少。
str_starts_with ( '商品错误类型为 type error' , '商品错误类型' ) ;
str_ends_with ( '商品错误类型为 type error' , 'type error' ) ;
最具潜力的特性:注解、Match表达式、WeakMap
注解
现在可以用原生的PHP语法来使用结构化的元数据,而不需要再依赖PHPDoc解析,性能也随之提升。之前定义注解路由可能需要使用:
class PostsController
{
public function get ( $id ) { }
}
现在你可以直接用PHP的注解语法来定义,并通过反射直接获取
class PostsController
{
public function get ( $id ) { }
}
Match表达式
你可以称它为switch表达式的大哥:match可以返回值,不需要break语句,可以组合条件,使用严格的类型比较,并且不执行任何类型的强制。
如下所示:
$result = match ( $input ) {
0 = > $this - > getZereResult ( ) ,
'1' , '2' , '3' = > $this - > getOtherNum ( ) ,
} ;
WeakMap
WeakMap保留对对象的引用,这些引用不会阻止这些对象被垃圾回收。
以 ORM 为例,它们通常实现缓存,这些缓存保存对实体类的引用,以提高实体之间的关系性能。
这些实体对象不能被垃圾回收,只要此缓存具有对它们的引用,即使缓存是唯一引用它们的对象。
如果此缓存层使用弱引用和映射代替,PHP 将垃圾收集这些对象当再没有别的引用他们了。
特别是在 ORM 的情况下,它可以管理请求中的数百个,
如果不是数千个实体;weak maps可以提供更好、更资源友好的处理这些对象的方法。
下面是weak maps的示例:
class Foo
{
private WeakMap $cache ;
public function getSomethingWithCaching ( object $obj ) : object
{
return $this - > cache [ $obj ]
? ? = $this - > computeSomethingExpensive ( $obj ) ;
}
}
其它特性
0 == 'test' 终于返回了false
我们知道在PHP7里面
0 == 'test' // 返回true
现在终于看起来更比较符合逻辑了
0 == 'test' // 返回false
可以在对象上使用::class
一个小而有用的新特性:现在可以对对象使用::class,它的工作方式与 get_class() 相同。
$test = new Test();
var_dump($test::class);
traits 中的抽象方法改进
Traits 可以指定抽象方法,这些方法必须由使用它们的类实现。在PHP8,必须保持一致的方法定义,包括参数类型和返回类型。
trait MyTrait {
abstract private function neededByTheTrait ( ) : string;
public function doSomething ( ) {
return strlen ( $this - > neededByTheTrait ( ) ) ;
}
}
class TraitUser {
use MyTrait ;
private function neededByTheTrait ( ) : string { }
private function neededByTheTrait ( ) : stdClass { }
private static function neededByTheTrait ( ) : string { }
}