PHP8.1的十大新功能,快用起来吧!

 2936

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 。它是一种逐项类型,包含固定数量的可能值。请参阅以下代码片段以了解如何使用枚举。

  1. <?php
  2.  
  3. /**
  4.  * Declare an enumeration.
  5.  * It can also contain an optional 'string' or 'int' value. This is called backed Enum.
  6.  * Backed enums (if used) should match the following criteria:
  7.  * - Declare the scalar type, whether string or int, in the Enum declaration.
  8.  * - All cases have values.
  9.  * - All cases contain the same scalar type, whether string or int.
  10.  * - Each case has a unique value.
  11.  */
  12. enum UserRole: string {
  13.     case ADMIN    = '1';
  14.     case GUEST    = '2';
  15.     case WRITER   = '3';
  16.     case EDITOR   = '4';
  17. }
  18.  
  19. /**
  20.  * You can access a case by using
  21.  * the '::' scope resolution operator.
  22.  * And, to get the name of the enum case, you
  23.  * can use the '->' followed by the attribute 'name'.
  24.  */
  25. echo UserRole::WRITER->name;
  26.  
  27. /**
  28.  * To get the value of the enum case, you can
  29.  * use the '->' followed by the attribute 'value'.
  30.  */
  31. echo UserRole::WRITER->value;
  32.  
  33. ?>


2. Fiber(纤维)

PHP 8.1 添加了对 Fiber 的支持,这是一个低级组件,允许在 PHP 中执行并发代码。Fiber 是一个代码块,它包含自己的变量和状态堆栈。这些 Fiber 可以被视为应用程序线程,可以从主程序启动。一旦启动,主程序将无法挂起或终止 Fiber。它只能从 Fiber 代码块内部暂停或终止。在 Fiber 挂起后,控制权再次返回到主程序,它可以从挂起的点继续执行 Fiber。

Fiber 本身不允许同时执行多个 Fiber 或主线程和一个 Fiber。但是,对于 PHP 框架来说,高效管理执行堆栈并允许异步执行是一个巨大的优势。

请参阅以下代码片段以了解如何使用 Fiber。

  1. <?php
  2.  
  3. /**
  4.  * Initialize the Fiber.
  5.  */
  6. $fiber = new Fiber(function(): void {
  7.     /**
  8.      * Print some message from inside the Fiber.
  9.      * Before the Fiber gets suspended.
  10.      */
  11.     echo "Welcome to Fiber!\n";
  12.     /**
  13.      * Suspend the Fiber.
  14.      */
  15.     Fiber::suspend();
  16.     /**
  17.      * Print some message from inside the Fiber.
  18.      * After the Fiber gets resumed.
  19.      */
  20.     echo "Welcome back to Fiber!\n";
  21. });
  22.  
  23. /**
  24.  * Print a message before starting a Fiber.
  25.  */
  26. echo "Starting a Fiber\n";
  27. /**
  28.  * Start the Fiber.
  29.  */
  30. $fiber->start();
  31. /**
  32.  * Fiber has been suspened from the inside.
  33.  * Print some message, and then resume the Fiber.
  34.  */
  35. echo "Fiber has been suspended\n";
  36. echo "Resuming the Fiber\n";
  37. /**
  38.  * Resume the Fiber.
  39.  */
  40. $fiber->resume();
  41. /**
  42.  * End of the example.
  43.  */
  44. echo "Fiber completed execution\n";
  45.  
  46. ?>


3.never返回类型

PHP 8.1 添加了名为never的返回类型。该never类型可用于指示函数将在执行一组指定的任务后终止程序执行。这可以通过抛出异常、调用exit()die()函数来完成。

never返回类型类似于void返回类型。但是,void返回类型在函数完成一组指定的任务后继续执行。

请参阅以下代码片段以了解如何使用 never 返回类型。

  1. <?php
  2.  
  3. /**
  4.  * Route Class
  5.  */
  6. class Route {
  7.  
  8.     /**
  9.      * Constructor of the class
  10.      * @return void
  11.      */
  12.     public function __construct() {
  13.  
  14.     }
  15.  
  16.     /**
  17.      * Redirect To a Page
  18.      * This function redirects to an URL specified by the user.
  19.      * @method redirect()
  20.      * @param string $url
  21.      * @param integer $httpCode
  22.      * @author Tara Prasad Routray <someemailaddress@example.com>
  23.      * @access public
  24.      * @return never
  25.      */
  26.     public static function redirect($url, $httpCode = 301): never {
  27.         /**
  28.          * Redirect to the URL specified.
  29.          */
  30.         header("Location: {$url}", true, $httpCode);
  31.         die;
  32.     }
  33. }
  34.  
  35. Route::redirect('https://www.google.com');
  36.  
  37. ?>


4.readonly属性

PHP 8.1 添加了名为readonly的类属性。已声明为只读的类属性只能初始化一次。里面设置的值不能改变。如果尝试强行更新该值,应用程序将抛出错误。请参阅以下代码片段以了解如何使用只读属性。

  1. <?php
  2.  
  3. /**
  4.  * User Class
  5.  */
  6. class User {
  7.     /**
  8.      * Declare a variable with readonly property.
  9.      * @var $authUserID
  10.      * @access public
  11.      */
  12.     public readonly int $authUserID;
  13.     /**
  14.      * Constructor of the class.
  15.      * @param integer $userID
  16.      * @return void
  17.      */
  18.     public function __construct($userID) {
  19.         /**
  20.          * Change the value of the property as specified.
  21.          * Updating the value of readonly properties are
  22.          * allowed only through the constructor.
  23.          */
  24.         $this->authUserID = $userID;
  25.     }
  26.     /**
  27.      * Update Auth User ID
  28.      * This function tries to update the readonly property (which is not allowed).
  29.      * @method updateAuthUserID()
  30.      * @param integer $userID
  31.      * @author Tara Prasad Routray <someemailaddress@example.com>
  32.      * @access public
  33.      * @return void
  34.      */
  35.     public function updateAuthUserID($userID) {
  36.         /**
  37.          * Change the value of the property as specified.
  38.          * Executing this function will throw the following error;
  39.          * PHP Fatal error:  Uncaught Error: Cannot modify readonly property User::$authUserID
  40.          */
  41.         $this->authUserID = $userID;
  42.     }
  43. }
  44. /**
  45.  * Initialize the class and update the value of the readonly property.
  46.  */
  47. $user = new User(30);
  48. /**
  49.  * Print the readonly property value.
  50.  * This will print 30.
  51.  */
  52. echo $user->authUserID;
  53. /**
  54.  * Call another function inside the class and try to update the class property.
  55.  */
  56. $user->updateAuthUserID(50);
  57. /**
  58.  * Print the readonly property value.
  59.  */
  60. echo $user->authUserID;
  61.  
  62. ?>


5. final类常量

PHP 8.1 添加了对名为final的类常量的支持。最终类常量不能被修改,即使是通过继承,这意味着它们不能被子类扩展或覆盖。

这个标志不能用于私有常量,因为它不能在类之外被访问。声明 final 和 private 常量将导致致命错误。

请参阅以下代码片段以了解如何使用最终标志。

  1. <?php
  2.  
  3. /**
  4.  * UserRole Class
  5.  */
  6. class UserRole {
  7.     /**
  8.      * Declare a final class constant with a value.
  9.      */
  10.     final public const ADMIN = '1';
  11. }
  12.  
  13. /**
  14.  * User Class extending the UserRole Class
  15.  */
  16. class User extends UserRole {
  17.     /**
  18.      * Declare another constant with the same name
  19.      * as of the parent class to override the value.
  20.      * 
  21.      * Note: Overriding the value will throw the following error:
  22.      * PHP Fatal error:  User::ADMIN cannot override final constant UserRole::ADMIN
  23.      */
  24.     public const ADMIN = '2';
  25. }
  26.  
  27. ?>


6. 新的 array_is_list() 函数

PHP 8.1 添加了名为array_is_list()的数组函数。它标识指定的数组是否具有从 0 开始的所有连续整数。如果数组是值的语义列表(一个数组,其键从 0 开始,都是整数,并且之间没有间隙),则此函数返回 true。对于空数组,它也返回 true。请参阅以下代码片段以了解如何使用 array_is_list() 函数。

  1. <?php
  2.  
  3. /**
  4.  * Returns true for empty array.
  5.  */
  6. array_is_list([]);
  7. /**
  8.  * Returns true for sequential set of keys.
  9.  */
  10. array_is_list([1, 2, 3]);
  11. /**
  12.  * Returns true as the first key is zero, and keys are in sequential order.
  13.  * It is same as [0 => 'apple', 1 => 2, 2 => 3]
  14.  */
  15. array_is_list(['apple', 2, 3]);
  16. /**
  17.  * Returns true as the first key is zero, and keys are in sequential order.
  18.  * It is same as [0 => 'apple', 1 => 'scissor']
  19.  */
  20. array_is_list(['apple', 'orange']);
  21. /**
  22.  * Returns true as the first key is zero, and keys are in sequential order.
  23.  * It is same as [0 => 'apple', 1 => 'scissor']
  24.  */
  25. array_is_list([0 => 'apple', 'orange']);
  26. /**
  27.  * Returns true as the first key is zero, and keys are in sequential order.
  28.  */
  29. array_is_list([0 => 'rock', 1 => 'scissor']);
  30.  
  31. ?>

键不是从 0 开始的数组,或者键不是整数,或者键是整数但不按顺序出现的数组将评估为 false。

  1. <?php
  2.  
  3. /**
  4.  * Returns false as the first key does not start from zero.
  5.  */
  6. array_is_list([1 => 'apple', 'orange']);
  7. /**
  8.  * Returns false as the first key does not start from zero.
  9.  */
  10. array_is_list([1 => 'apple', 0 => 'orange']);
  11. /**
  12.  * Returns false as all keys are not integer.
  13.  */
  14. array_is_list([0 => 'apple', 'fruit' => 'orange']);
  15. /**
  16.  * Returns false as the keys are not in sequential order.
  17.  */
  18. array_is_list([0 => 'apple', 2 => 'orange']); 
  19.  
  20. ?>


7. 新的fsync()fdatasync()函数

PHP 8.1 添加了对fsync()fdatasync()函数的支持。两者都与现有fflush()函数有相似之处,该函数当前用于将缓冲区刷新到操作系统中。然而,fsync()fdatasync()刷新该缓冲区到物理存储。它们之间的唯一区别是该fsync()函数在同步文件更改时包含元数据,而该fdatasync()函数不包含元数据。

fsync()函数将采用文件指针并尝试将更改提交到磁盘。成功时返回 true,失败时返回 false,如果资源不是文件,则会发出警告。fdatasync()函数的工作方式相同,但速度稍快一些,因为 fsync() 将尝试完全同步文件的数据更改和有关文件的元数据(上次修改时间等),这在技术上是两次磁盘写入。

请参阅以下代码片段以了解如何使用 fsync() 和 fdatasync() 函数。

  1. <?php
  2.  
  3. /**
  4.  * Declare a variable and assign a filename.
  5.  */
  6. $fileName = 'notes.txt';
  7. /**
  8.  * Create the file with read and write permission.
  9.  */
  10. $file = fopen($fileName, 'w+');
  11. /**
  12.  * Add some text into the file.
  13.  */
  14. fwrite($file, 'Paragraph 1');
  15. /**
  16.  * Add a line break into the file.
  17.  */
  18. fwrite($file, "\r\n");
  19. /**
  20.  * Add some more text into the file.
  21.  */
  22. fwrite($file, 'Paragraph 2');
  23. /**
  24.  * You can use both the fsync() or fdatasync() functions 
  25.  * to commit changs to disk.
  26.  */
  27. fsync($file); // or fdatasync($file).
  28. /**
  29.  * Close the open file pointer.
  30.  */
  31. fclose($file);
  32.  
  33. ?>


8. 对字符串键数组解包的支持

PHP 8.1 添加了对字符串键数组解包的支持。为了解压数组,PHP 使用展开(…)运算符。PHP 7.4 中引入了这个运算符来合并两个或多个数组,但语法更简洁。但在 PHP 8.1 之前,展开运算符仅支持带数字键的数组。请参阅以下代码片段以了解如何将展开运算符用于字符串键控数组。

  1. <?php
  2.  
  3. /**
  4.  * Declare an array
  5.  */
  6. $fruits1 = ['Jonathan Apples', 'Sapote'];
  7. /**
  8.  * Declare another array
  9.  */
  10. $fruits2 = ['Pomelo', 'Jackfruit'];
  11. /**
  12.  * Merge above two arrays using array unpacking.
  13.  */
  14. $unpackedFruits = [...$fruits1, ...$fruits2, ...['Red Delicious']];
  15. /**
  16.  * Print the above unpacked array.
  17.  * This will print:
  18.  * array(5) {
  19.  * [0]=>
  20.  * string(15) "Jonathan Apples"
  21.  * [1]=>
  22.  * string(6) "Sapote"
  23.  * [2]=>
  24.  * string(6) "Pomelo"
  25.  * [3]=>
  26.  * string(9) "Jackfruit"
  27.  * [4]=>
  28.  * string(13) "Red Delicious"
  29.  * }
  30.  */
  31. var_dump($unpackedFruits);
  32.  
  33. ?>


9. $_FILES 新的用于目录上传的 full_path 键

PHP 8.1 添加了对$_FILES全局变量中full_path新键的支持。在 PHP 8.1 之前,$_FILES没有存储到服务器的相对路径或确切目录。因此,您无法使用 HTML 文件上传表单上传整个目录。新full_path键解决了这个问题。它存储相对路径并在服务器上重建确切的目录结构,使目录上传成为可能。请参阅以下代码片段以了解如何将full_path键与$_FILES全局变量一起使用。

  1. <?php
  2.  
  3. /**
  4.  * Check if the user has submitted the form.
  5.  */
  6. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  7.     /**
  8.      * Print the $_FILES global variable. This will display the following:
  9.      * array(1) {
  10.      *   ["myfiles"]=> array(6) {
  11.      *     ["name"]=> array(2) {
  12.      *       [0]=> string(9) "image.png"
  13.      *       [1]=> string(9) "image.png"
  14.      *     }
  15.      *     ["full_path"]=> array(2) {
  16.      *       [0]=> string(25) "folder1/folder2/image.png"
  17.      *       [1]=> string(25) "folder3/folder4/image.png"
  18.      *     }
  19.      *     ["tmp_name"]=> array(2) {
  20.      *       [0]=> string(14) "/tmp/phpV1J3EM"
  21.      *       [1]=> string(14) "/tmp/phpzBmAkT"
  22.      *     }
  23.      *     // ... + error, type, size
  24.      *   }
  25.      * }
  26.      */
  27.     var_dump($_FILES);
  28. }
  29.  
  30. ?>
  31.  
  32. <form action="" method="POST" enctype="multipart/form-data">
  33.     <input name="myfiles[]" type="file" webkitdirectory multiple />
  34.     <button type="submit">Submit</button>
  35. </form>


10. 新的IntlDatePatternGenerator

PHP 8.1 添加了对新IntlDatePatternGenerator类的支持。在 PHP 8.1 之前,只能使用IntlDateFormatter。虽然它支持昨天、今天和明天使用的八种预定义格式,但是这些格式和IntlDatePatternGenerator不太一样。这个类允许指定日期、月份和时间的格式,并且顺序将由类自动处理。请参阅以下代码片段以了解如何使用 IntlDatePatternGenerator 类。

  1. <?php
  2.  
  3. /**
  4.  * Define a default date format.
  5.  */
  6. $skeleton = "YYYY-MM-dd";
  7. /**
  8.  * Parse a time string (for today) according to a specified format.
  9.  */
  10. $today = \DateTimeImmutable::createFromFormat('Y-m-d', date('Y-m-d'));
  11. /**
  12.  * ===========================
  13.  * PRINTING DATE IN USA FORMAT
  14.  * ===========================
  15.  * Initiate an instance for the IntlDatePatternGenerator class
  16.  * and provide the locale information.
  17.  * In the below example, I've used locale: en_US.
  18.  */
  19. $intlDatePatternGenerator = new \IntlDatePatternGenerator("en_US");
  20. /**
  21.  * Get the correct date format for the locale: en_US.
  22.  * Following function "getBestPattern" will return:
  23.  * MM/dd/YYYY
  24.  */
  25. $enUSDatePattern = $intlDatePatternGenerator->getBestPattern($skeleton);
  26. /**
  27.  * Use the "formatObject" function of IntlDateFormatter to print as per specified pattern.
  28.  * This will print the following:
  29.  * Date in en-US: 12/03/2021
  30.  */
  31. echo "Date in en-US: ". \IntlDateFormatter::formatObject($today, $enUSDatePattern, "en_US"). "\n";
  32.  
  33. /**
  34.  * =============================
  35.  * PRINTING DATE IN INDIA FORMAT
  36.  * =============================
  37.  * Initiate an instance for the IntlDatePatternGenerator class
  38.  * and provide the locale information.
  39.  * In the below example, I've used locale: en_IN.
  40.  */
  41. $intlDatePatternGenerator = new \IntlDatePatternGenerator("en_IN");
  42. /**
  43.  * Get the correct date format for the locale: en_IN.
  44.  * Following function "getBestPattern" will return:
  45.  * dd/MM/YYYY
  46.  */
  47. $enINDatePattern = $intlDatePatternGenerator->getBestPattern($skeleton);
  48. /**
  49.  * Use the "formatObject" function of IntlDateFormatter to print as per specified pattern.
  50.  * This will print the following:
  51.  * Date in en-IN: 03/12/2021
  52.  */
  53. echo "Date in en-IN: ". \IntlDateFormatter::formatObject($today, $enINDatePattern, "en_IN"). "\n";
  54.  
  55. ?>

点赞!您已经完成了 PHP 8.1 提供的功能的学习。现在您可以继续并开始在您当前或即将进行的项目中实现上述功能。

原文:https://levelup.gitconnected.com/top-10-php-8-1-features-you-should-start-using-now-7161b91275fd


TAG标签:
本文网址:https://www.zztuku.com/detail-10367.html
站长图库 - PHP8.1的十大新功能,快用起来吧!
申明:本文转载于《learnku》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐

    各种各样的眼镜矢量素材