• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧
您的位置:首页 > CMS教程 >建站教程 > PHP8.1的十大新功能,快用起来吧!

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

作者:站长图库 字体:[增加 减小] 来源:互联网

站长图库向大家介绍了PHP8.1新功能等相关知识,希望对您有所帮助

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&
  


 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

相关文章

  • vue路由学习之区分$route和$router,看看它们的区别
  • 织梦后台非常卡,经常无反应怎么办
  • 完美解决更新和发布文章出现Unknown column 'weight
  • 帝国CMS提示Table 'XX.***_enewspubtemp_2' doesn't exist错误详解
  • PhotoShop滤镜制作简单的冰晶字效果教程
  • 介绍PHP基于Thinkphp5的砍价活动相关设计
  • 帝国CMS后台密码忘了怎么办,找回密码的两种方法
  • PHP开发支付宝PC扫码支付/支付宝当面付开发流程
  • PHP怎么设置上传图片大小
  • 帝国CMS内容字段域名替换函数

文章分类

  • dedecms
  • ecshop
  • z-blog
  • UcHome
  • UCenter
  • drupal
  • WordPress
  • 帝国cms
  • phpcms
  • 动易cms
  • phpwind
  • discuz
  • 科汛cms
  • 风讯cms
  • 建站教程
  • 运营技巧

最近更新的内容

    • 百度与谷歌在SEO表现上的差别
    • Ajax实现登录案例
    • 好看短视频解析下载Python脚本
    • Discuz!您当前的访问请求当中含有非法字符,已经被系统拒绝解决办法
    • Photoshop创建简洁绚丽的几何组合背景
    • 介绍thinkPHP配置虚拟域名简化URL路径
    • PhotoShop绘制水晶质感3D立体按钮制作教程
    • YII怎么输出sql语句?
    • PS鼠绘清纯可爱的古装卡通小女孩
    • PHP高并发实例详解之解决商品库存超卖问题

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有