PHP8所有新特性
2025-01-29
PHP 8 正式版即将发布,是时候来看看 PHP 8 即将推出的新特性了
首先来安装PHP8
下载地址
https://www.php.net/downloads
本地编译安装 PHP 8 RC2 版本(MAC操作系统)
# 0、下载解压源码
wget https://downloads.php.net/~pollita/php-8.0.0RC2.tar.gz
tar zxvf php-8.0.0RC2.tar.gz
cd php-8.0.0RC2
# 1、生成 configure 文件
./buildconf --force
# 2、配置构建流程(最小化安装)
./configure --prefix=/usr/local/php8 \
--with-config-file-path=/usr/local/php8 \
--enable-cli \
--without-iconv
# 3、构建 && 安装
make && sudo make install
# 4、拷贝配置文件
sudo cp php.ini-development /usr/local/php8/php.ini
验证是否安装成功命令
/usr/local/php8/bin/php -v
利用别名来简化命令:
alias php8="/usr/local/php8/bin/php"
之后上述命令可简化为:
php8 -v
下面来总结下新特性
- 新增对联合类型的支持
联合类型允许一个变量拥有多个类型的值,而不是一个(参考 C 语言的联合类型很好理解)。
考虑到 PHP 动态语言类型的特性,现在很多情况下,联合类型都是很有用的。联合类型是两个或者多个类型的集合,表示可以使用其中任何一个类型。
public function foo(Foo|Bar $input): int|float;
请注意,联合类型中不包含 void,因为 void 表示的含义是 “根本没有返回值”。 另外,可以使用 |null 或者现有的 ? 表示法来表示包含 nullable 的联合体 :
public function foo(Foo|null $foo): void;
public function bar(?Bar $bar): void;
-
新增 WeakMap (弱映射)特性
WeakMap 允许你创建对象到任意值的映射(类似 SplObjectStorage),同时也不会阻止作为键的对象被垃圾回收。如果某个对象键被垃圾回收,对应键值对将从集合中移除。这一新特性非常有用,因为这样一来,开发者就不必担心代码存在内存泄露了。大多数 PHP 开发者可能对此并不关心,但是当你编写长时间运行的进程时一定要提防这个问题,比如使用 ReactPHP 进行事件驱动编程时:有了 WeakMap 后,引用的对象会在失效时自动被垃圾回收。
如果你在数组中做同样的事情,则仍然会持有该对象的引用,从而导致内存泄露。
下面是弱映射基本的例子,RFC :
class FooBar {
private WeakMap $cache;
public function getSomethingWithCaching(object $obj) {
return $this->cache[$obj] ??= $this->computeSomethingExpensive($obj);
}
// ...
}
- 新增 ValueError 异常
PHP 8 引入了新的名为 ValueError 的内置异常类,它继承自 Exception 基类。每次当你传递值到函数时,如果是一个无效类型,则会抛出该异常,在 PHP 8 之前,这样的操作会导致警告。 - 重写方法时允许可变参数
当我们在子类重写父类方法时,任何数量的参数现在都可以被替换成可变参数,只要对应参数类型是兼容的即可:
<?php
class A {
public function method(int $many, string $parameters, $here) {
}
}
class B extends A {
public function method(...$everything) {
}
}
- 新增 static 返回类型
尽管已经可以返回 self,但是 static 直到 PHP 8 才是有效的返回类型 。考虑到 PHP 具有动态类型的性质,此功能对于许多开发人员将非常有用。
class Foo
{
public function test(): static
{
return new static();
}
}
- 允许对对象使用 ::class
PHP 8 中可以使用 o b j e c t : : c l a s s 获 取 对 象 的 类 名 , 其 返 回 结 果 和 g e t c l a s s ( object::class 获取对象的类名,其返回结果和 get_class( object::class获取对象的类名,其返回结果和getclass(object) 一样:
$foo = new Foo();
var_dump($foo::class);
- 变量语法调整
new 和 instanceof 关键字现在可以被用于任意表达式:
class Foo {}
class Bar {}
$names = ['Foo', 'Bar'];
$class = new ($names[array_rand($names)]);
var_dump($class);
- 新增 Stringable 接口
Stringable 接口可用于键入提示任何字符串或实现__ toString() 的内容。此外,每当一个类实现__ toString() 时,即被视作自动实现了 Stringable 接口,而无需手动实现。
class Foo {
public function __toString() {
return 'I am a class';
}
}
$obj = new Foo;
var_dump($obj instanceof Stringable);
- Trait 现在可以定义抽象私有方法
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 { }
}
- throw 现在可以被用作表达式
// This was previously not possible since arrow functions only accept a single expression while throw was a statement.
$callable = fn() => throw new Exception();
// $value is non-nullable.
$value = $nullableValue ?? throw new InvalidArgumentException();
// $value is truthy.
$value = $falsableValue ?: throw new InvalidArgumentException();
// $value is only set if the array is not empty.
$value = !empty($array)
? reset($array)
: throw new InvalidArgumentException();
- 参数列表中允许出现可选的尾部逗号
和数组中的尾部逗号类似,现在也可以在参数列表中定义一个尾部逗号:
function method_with_many_arguments(
$a,
$b,
$c,
$d,
) {
var_dump("这是个有效语法");
}
method_with_many_arguments(
1,
2,
3,
4,
);
- 捕获异常而不存储到变量
现在可以编写 catch (Exception) 代码来捕获异常而不必将其存储到一个变量中:
$nullableValue = null;
try {
$value = $nullableValue ?? throw new \InvalidArgumentException();
} catch (\InvalidArgumentException) {
var_dump("Something went wrong");
}
- 新增对 mixed 类型的支持
PHP 8 引入了新的名为 mixed 的类型,该类型等价于
array | bool | callable | int | float | null | object | resource | string |
---|
请注意,mixed 不仅仅可以用来作为返回类型,还可以用作参数和属性类型。
另外,还需要注意,因为 mixed 类型已经包括了 null,因此 mixed 类型不可为空。下面的代码会触发致命错误:
// 致命错误:混合类型不能为空,null已经是混合类型的一部分。
function bar(): ?mixed {}
- 新增对注解的支持
PHP 8 的注解实际上包含了多个 RFC:
https://wiki.php.net/rfc/attributes_v2
https://wiki.php.net/rfc/attribute_amendments
https://wiki.php.net/rfc/shorter_attribute_syntax
https://wiki.php.net/rfc/shorter_attribute_syntax_change
注解绝对是 PHP 8 引入的最大新特性之一,一开始理解起来可能有点困难(不过有 Java 基础的话会很简单)。简而言之,注解允许你添加元数据到 PHP 函数、参数、类等,这些元数据随后可以通过可编程方式获取,在 PHP 7 或者更低版本中实现类似功能需要解析代码注释块,而通过注解可以直接访问深度集成到 PHP 自身的这些信息。
我们来编写一段示例代码方便你理解,假设你想要允许开发者添加中间件到控制器类/方法,使用注解可以这么做:
// 首先,我们需要定义注解,注解本身只是一个原生的 PHP 类,并且自身被打上了注解的注释
#[Attribute]
class ApplyMiddleware
{
public array $middlware = [];
public function __construct(...$middleware)
{
$this->middleware = $middleware;
}
}
// 下面的语法会添加上述注解到 MyController 类,并且传入 auth 作为参数
#[ApplyMiddleware('auth')]
class MyController
{
public function index()
{
}
}
// 然后我们就可以在类中使用反射获取所有的 ApplyMiddleware 注解并读取给定的中间件参数
$reflectionClass = new ReflectionClass(MyController::class);
$attributes = $reflectionClass->getAttributes(ApplyMiddleware::class);
foreach ($attributes as $attribute) {
$middlewareAttribute = $attribute->newInstance();
var_dump($middlewareAttribute->middleware);
}
- 新增构造函数属性提示支持
这个新特性只是一个语法简写而言,可以将属性声明和构造函数属性初始化合并到一起:
class Test {
public function __construct(
public int $id,
public string $name,
) {}
}
$user = new Test(1, 'Marcel');
- 新增 match 表达式支持
match 表达式和 switch 分支语句类似,但是语义上更加安全并且可以直接返回值:
echo match (1) {
0 => 'a',
1 => 'b',
2 => 'c',
};
- 新增对空安全运算符 ?-> 的支持
当该运算符的左侧评估为 null 时,整个代码链路的执行将会被终止并整体评估为 null。如果不为 null 的话,则和普通的 -> 运算符功能一致:
class User {
public function getAddress() {}
}
$user = new User();
$country = $user?->getAddress()?->country?->iso_code;
var_dump($country);
- 新增对命名参数的支持
命名参数允许基于参数名称传递参数到函数,而不是参数所在的位置,这样一来,函数参数就可以自解释并且与顺序无关,并且允许跳过默认值:
array_fill(start_index: 0, num: 100, value: 50);