PHP 8.1 现已推出,它附带了新功能和性能改进——最令人兴奋的是新的 JIT 编译器。它最近于 2021 年 11 月 25 日发布。
我将详细演示 PHP 8.1 提供的 10 大特性,以便您可以开始在您的项目中使用它们,并改善您的 PHP 体验。初学者和有经验的开发人员可以从本文中受益。
PHP 8.1 提供的 10 大功能
1、枚举
2、Fiber(纤维)
3、never返回类型
4、readonly属性
5、final类常量
6、新的array_is_list()函数
7、新的fsync()和fdatasync()函数
8、对字符串键数组解包的支持
9、$_FILES新的用于目录上传的full_path键
10、新的IntlDatePatternGenerator类
1. 枚举
PHP 8.1 添加了对枚举的支持,简写为 enum 。它是一种逐项类型,包含固定数量的可能值。请参阅以下代码片段以了解如何使用枚举。
<?php /** * Declare an enumeration. * It can also contain an optional 'string' or 'int' value. This is called backed Enum. * Backed enums (if used) should match the following criteria: * - Declare the scalar type, whether string or int, in the Enum declaration. * - All cases have values. * - All cases contain the same scalar type, whether string or int. * - Each case has a unique value. */enum UserRole: string { case ADMIN = '1'; case GUEST = '2'; case WRITER = '3'; case EDITOR = '4';} /** * You can access a case by using * the '::' scope resolution operator. * And, to get the name of the enum case, you * can use the '->' followed by the attribute 'name'. */echo UserRole::WRITER->name; /** * To get the value of the enum case, you can * use the '->' followed by the attribute 'value'. */echo UserRole::WRITER->value; ?>2. Fiber(纤维)
PHP 8.1 添加了对 Fiber 的支持,这是一个低级组件,允许在 PHP 中执行并发代码。Fiber 是一个代码块,它包含自己的变量和状态堆栈。这些 Fiber 可以被视为应用程序线程,可以从主程序启动。一旦启动,主程序将无法挂起或终止 Fiber。它只能从 Fiber 代码块内部暂停或终止。在 Fiber 挂起后,控制权再次返回到主程序,它可以从挂起的点继续执行 Fiber。
Fiber 本身不允许同时执行多个 Fiber 或主线程和一个 Fiber。但是,对于 PHP 框架来说,高效管理执行堆栈并允许异步执行是一个巨大的优势。
请参阅以下代码片段以了解如何使用 Fiber。
<?php /** * Initialize the Fiber. */$fiber = new Fiber(function(): void { /** * Print some message from inside the Fiber. * Before the Fiber gets suspended. */ echo "Welcome to Fiber!\n"; /** * Suspend the Fiber. */ Fiber::suspend(); /** * Print some message from inside the Fiber. * After the Fiber gets resumed. */ echo "Welcome back to Fiber!\n";}); /** * Print a message before starting a Fiber. */echo "Starting a Fiber\n";/** * Start the Fiber. */$fiber->start();/** * Fiber has been suspened from the inside. * Print some message, and then resume the Fiber. */echo "Fiber has been suspended\n";echo "Resuming the Fiber\n";/** * Resume the Fiber. */$fiber->resume();/** * End of the example. */echo "Fiber completed execution\n"; ?>3.never返回类型
PHP 8.1 添加了名为never的返回类型。该never类型可用于指示函数将在执行一组指定的任务后终止程序执行。这可以通过抛出异常、调用exit()或die()函数来完成。
never返回类型类似于void返回类型。但是,void返回类型在函数完成一组指定的任务后继续执行。
请参阅以下代码片段以了解如何使用 never 返回类型。
<?php /** * Route Class */class Route { /** * Constructor of the class * @return void */ public function __construct() { } /** * Redirect To a Page * This function redirects to an URL specified by the user. * @method redirect() * @param string $url * @param integer $httpCode * @author Tara Prasad Routray <someemailaddress@example.com> * @access public * @return never */ public static function redirect($url, $httpCode = 301): never { /** * Redirect to the URL specified. */ header("Location: {$url}", true, $httpCode); die; }} Route::redirect('https://www.google.com'); ?>4.readonly属性
PHP 8.1 添加了名为readonly的类属性。已声明为只读的类属性只能初始化一次。里面设置的值不能改变。如果尝试强行更新该值,应用程序将抛出错误。请参阅以下代码片段以了解如何使用只读属性。
<?php /** * User Class */class User { /** * Declare a variable with readonly property. * @var $authUserID * @access public */ public readonly int&

