PHP8新特性
PHP 8.1 提供的功能
- 枚举
- Fiber(纤维)
- never 返回类型
- readonly 属性
- final 类常量
- 新的 array_is_list() 函数
- 新的 fsync() 和 fdatasync() 函数
- 对字符串键数组解包的支持
- $_FILES 新的用于目录上传的 full_path 键
- 新的 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 $authUserID;
/**
* Constructor of the class.
* @param integer $userID
* @return void
*/
public function __construct($userID) {
/**
* Change the value of the property as specified.
* Updating the value of readonly properties are
* allowed only through the constructor.
*/
$this->authUserID = $userID;
}
/**
* Update Auth User ID
* This function tries to update the readonly property (which is not allowed).
* @method updateAuthUserID()
* @param integer $userID
* @author Tara Prasad Routray <someemailaddress@example.com>
* @access public
* @return void
*/
public function updateAuthUserID($userID) {
/**
* Change the value of the property as specified.
* Executing this function will throw the following error;
* PHP Fatal error: Uncaught Error: Cannot modify readonly property User::$authUserID
*/
$this->authUserID = $userID;
}
}
/**
* Initialize the class and update the value of the readonly property.
*/
$user = new User(30);
/**
* Print the readonly property value.
* This will print 30.
*/
echo $user->authUserID;
/**
* Call another function inside the class and try to update the class property.
*/
$user->updateAuthUserID(50);
/**
* Print the readonly property value.
*/
echo $user->authUserID; ?>
5. final 类常量
PHP 8.1 添加了对名为 final 的类常量的支持。最终类常量不能被修改,即使是通过继承,这意味着它们不能被子类扩展或覆盖。
这个标志不能用于私有常量,因为它不能在类之外被访问。声明 final 和 private 常量将导致致命错误。
请参阅以下代码片段以了解如何使用最终标志。
<?php /**
* UserRole Class
*/
class UserRole {
/**
* Declare a final class constant with a value.
*/
final public const ADMIN = '1';
} /**
* User Class extending the UserRole Class
*/
class User extends UserRole {
/**
* Declare another constant with the same name
* as of the parent class to override the value.
*
* Note: Overriding the value will throw the following error:
* PHP Fatal error: User::ADMIN cannot override final constant UserRole::ADMIN
*/
public const ADMIN = '2';
} ?>
6. 新的 array_is_list() 函数
PHP 8.1 添加了名为 array_is_list() 的数组函数。它标识指定的数组是否具有从 0 开始的所有连续整数。如果数组是值的语义列表(一个数组,其键从 0 开始,都是整数,并且之间没有间隙),则此函数返回 true。对于空数组,它也返回 true。请参阅以下代码片段以了解如何使用 array_is_list () 函数。
<?php /**
* Returns true for empty array.
*/
array_is_list([]);
/**
* Returns true for sequential set of keys.
*/
array_is_list([1, 2, 3]);
/**
* Returns true as the first key is zero, and keys are in sequential order.
* It is same as [0 => 'apple', 1 => 2, 2 => 3]
*/
array_is_list(['apple', 2, 3]);
/**
* Returns true as the first key is zero, and keys are in sequential order.
* It is same as [0 => 'apple', 1 => 'scissor']
*/
array_is_list(['apple', 'orange']);
/**
* Returns true as the first key is zero, and keys are in sequential order.
* It is same as [0 => 'apple', 1 => 'scissor']
*/
array_is_list([0 => 'apple', 'orange']);
/**
* Returns true as the first key is zero, and keys are in sequential order.
*/
array_is_list([0 => 'rock', 1 => 'scissor']); ?>
键不是从 0 开始的数组,或者键不是整数,或者键是整数但不按顺序出现的数组将评估为 false。
<?php /**
* Returns false as the first key does not start from zero.
*/
array_is_list([1 => 'apple', 'orange']);
/**
* Returns false as the first key does not start from zero.
*/
array_is_list([1 => 'apple', 0 => 'orange']);
/**
* Returns false as all keys are not integer.
*/
array_is_list([0 => 'apple', 'fruit' => 'orange']);
/**
* Returns false as the keys are not in sequential order.
*/
array_is_list([0 => 'apple', 2 => 'orange']); ?>
7. 新的 fsync() 和 fdatasync() 函数
PHP 8.1 添加了对 fsync() 和 fdatasync() 函数的支持。两者都与现有 fflush() 函数有相似之处,该函数当前用于将缓冲区刷新到操作系统中。然而,fsync() 和 fdatasync() 刷新该缓冲区到物理存储。它们之间的唯一区别是该 fsync() 函数在同步文件更改时包含元数据,而该 fdatasync() 函数不包含元数据。
fsync() 函数将采用文件指针并尝试将更改提交到磁盘。成功时返回 true,失败时返回 false,如果资源不是文件,则会发出警告。fdatasync() 函数的工作方式相同,但速度稍快一些,因为 fsync () 将尝试完全同步文件的数据更改和有关文件的元数据(上次修改时间等),这在技术上是两次磁盘写入。
请参阅以下代码片段以了解如何使用 fsync () 和 fdatasync () 函数。
<?php /**
* Declare a variable and assign a filename.
*/
$fileName = 'notes.txt';
/**
* Create the file with read and write permission.
*/
$file = fopen($fileName, 'w+');
/**
* Add some text into the file.
*/
fwrite($file, 'Paragraph 1');
/**
* Add a line break into the file.
*/
fwrite($file, "\r\n");
/**
* Add some more text into the file.
*/
fwrite($file, 'Paragraph 2');
/**
* You can use both the fsync() or fdatasync() functions
* to commit changs to disk.
*/
fsync($file); // or fdatasync($file).
/**
* Close the open file pointer.
*/
fclose($file); ?>
8. 对字符串键数组解包的支持
PHP 8.1 添加了对字符串键数组解包的支持。为了解压数组,PHP 使用展开 (…) 运算符。PHP 7.4 中引入了这个运算符来合并两个或多个数组,但语法更简洁。但在 PHP 8.1 之前,展开运算符仅支持带数字键的数组。请参阅以下代码片段以了解如何将展开运算符用于字符串键控数组。
<?php /**
* Declare an array
*/
$fruits1 = ['Jonathan Apples', 'Sapote'];
/**
* Declare another array
*/
$fruits2 = ['Pomelo', 'Jackfruit'];
/**
* Merge above two arrays using array unpacking.
*/
$unpackedFruits = [...$fruits1, ...$fruits2, ...['Red Delicious']];
/**
* Print the above unpacked array.
* This will print:
* array(5) {
* [0]=>
* string(15) "Jonathan Apples"
* [1]=>
* string(6) "Sapote"
* [2]=>
* string(6) "Pomelo"
* [3]=>
* string(9) "Jackfruit"
* [4]=>
* string(13) "Red Delicious"
* }
*/
var_dump($unpackedFruits); ?>
9. $_FILES 新的用于目录上传的 full_path 键
PHP 8.1 添加了对 $_FILES 全局变量中 full_path 新键的支持。在 PHP 8.1 之前,$_FILES 没有存储到服务器的相对路径或确切目录。因此,您无法使用 HTML 文件上传表单上传整个目录。新 full_path 键解决了这个问题。它存储相对路径并在服务器上重建确切的目录结构,使目录上传成为可能。请参阅以下代码片段以了解如何将 full_path 键与 $_FILES 全局变量一起使用。
<?php /**
* Check if the user has submitted the form.
*/
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
/**
* Print the $_FILES global variable. This will display the following:
* array(1) {
* ["myfiles"]=> array(6) {
* ["name"]=> array(2) {
* [0]=> string(9) "image.png"
* [1]=> string(9) "image.png"
* }
* ["full_path"]=> array(2) {
* [0]=> string(25) "folder1/folder2/image.png"
* [1]=> string(25) "folder3/folder4/image.png"
* }
* ["tmp_name"]=> array(2) {
* [0]=> string(14) "/tmp/phpV1J3EM"
* [1]=> string(14) "/tmp/phpzBmAkT"
* }
* // ... + error, type, size
* }
* }
*/
var_dump($_FILES);
} ?> <form action="" method="POST" enctype="multipart/form-data">
<input name="myfiles[]" type="file" webkitdirectory multiple />
<button type="submit">Submit</button>
</form>
10. 新的 IntlDatePatternGenerator 类
PHP 8.1 添加了对新 IntlDatePatternGenerator 类的支持。在 PHP 8.1 之前,只能使用 IntlDateFormatter。虽然它支持昨天、今天和明天使用的八种预定义格式,但是这些格式和 IntlDatePatternGenerator 不太一样。这个类允许指定日期、月份和时间的格式,并且顺序将由类自动处理。请参阅以下代码片段以了解如何使用 IntlDatePatternGenerator 类。
<?php /**
* Define a default date format.
*/
$skeleton = "YYYY-MM-dd";
/**
* Parse a time string (for today) according to a specified format.
*/
$today = \DateTimeImmutable::createFromFormat('Y-m-d', date('Y-m-d'));
/**
* ===========================
* PRINTING DATE IN USA FORMAT
* ===========================
* Initiate an instance for the IntlDatePatternGenerator class
* and provide the locale information.
* In the below example, I've used locale: en_US.
*/
$intlDatePatternGenerator = new \IntlDatePatternGenerator("en_US");
/**
* Get the correct date format for the locale: en_US.
* Following function "getBestPattern" will return:
* MM/dd/YYYY
*/
$enUSDatePattern = $intlDatePatternGenerator->getBestPattern($skeleton);
/**
* Use the "formatObject" function of IntlDateFormatter to print as per specified pattern.
* This will print the following:
* Date in en-US: 12/03/2021
*/
echo "Date in en-US: ". \IntlDateFormatter::formatObject($today, $enUSDatePattern, "en_US"). "\n"; /**
* =============================
* PRINTING DATE IN INDIA FORMAT
* =============================
* Initiate an instance for the IntlDatePatternGenerator class
* and provide the locale information.
* In the below example, I've used locale: en_IN.
*/
$intlDatePatternGenerator = new \IntlDatePatternGenerator("en_IN");
/**
* Get the correct date format for the locale: en_IN.
* Following function "getBestPattern" will return:
* dd/MM/YYYY
*/
$enINDatePattern = $intlDatePatternGenerator->getBestPattern($skeleton);
/**
* Use the "formatObject" function of IntlDateFormatter to print as per specified pattern.
* This will print the following:
* Date in en-IN: 03/12/2021
*/
echo "Date in en-IN: ". \IntlDateFormatter::formatObject($today, $enINDatePattern, "en_IN"). "\n"; ?>
Attributes(注解)
注解语法
注解是语法,而不是注释。注解有如下几种形式:
语法:#[]
#[Name]
#[Name1, Name2,...]
#[Name(Arguments)]
#[Name(Arguments1, Arguments2, ArgumentsN)]
#[Name1(Arguments), Name2(Arguments)]
注解类型
TARGET_CLASS //类的注解类
TARGET_FUNCTION //函数注解类
TARGET_METHOD //方法注解类
TARGET_PROPERTY //属性注解类
TARGET_CLASS_CONSTANT //类常量注解类
TARGET_PARAMETER //参数注解类
TARGET_ALL
使用注解三步走:
1)定义注解
2)使用注解
3)提取注释
基础案例
案例一:只有注解名
#[url]
function show() {
// 定义一个函数
// 其注释使用注解来表示
} // 使用反射获取注释信息
// 1、实例化函数反射类并传递函数名
$ref = new ReflectionFunction('show');
print_r($ref);
// 2、获取函数注解属性对象
$attr = $ref->getAttributes()[0];
// 3、获取注解名
print_r($attr->getName()); // 输出 url
案例二:带有参数
#[url('demo.com/id/10')]
function show() {
// 定义一个函数
// 其注释使用注解来表示
} $ref = new ReflectionFunction('show');
$attr = $ref->getAttributes()[0];
echo $attr->getName();
// 获取参数
print_r($attr->getArguments()); // 数组格式
注解案例
案例一:多个注解,对应着多个注解属性对象
#[url('demo.com/id/10'), param('name')]
function show() {} $ref = new ReflectionFunction('show');
$attr = $ref->getAttributes();
print_r($attr);
// 输出结果如下:
Array
(
[0] => ReflectionAttribute Object
(
// 该对象对应着 url注解
)
[1] => ReflectionAttribute Object
(
// 该对象对应着 param注解
)
)
案例二:注解中参数为数组
#[url('api/user/1', methods:['GET'])]
function getName($id) {} $ref = new ReflectionFunction('getName');
$attr = $ref->getAttributes()[0];
echo $attr->getName();
// 获取参数,其中methods为数组
print_r($attr->getArguments()); // 输出结果如下
url
Array (
[0] => api/user/1
[methods] => Array
(
[0] => GET
)
)
案例三:注解类型的使用
注解参数的使用
// 1、注解的目标为类或者函数
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_FUNCTION)]
class Person
{
public function __construct(string $name, int $age)
{
// 5、输出结果,api/user/1 10
echo $name, $age;
}
} // 2、注解函数
#[Person('api/user/1', 10)]
function getPerson() {} // 3、解析注解
$ref = new ReflectionFunction('getPerson');
$attr = $ref->getAttributes()[0];
// 4、实例化该注解函数
$attr->newInstance();
第一步:为Person声明了一个名为Attribute
的直接,其指向的注解目标指向一个函数
或类
;
第二步:定义一个函数,其注解名为Person
(此时,注解名必须和类名保持一致);
第三步:使用反射获取注解相关信息
第四步:调用值的实例化对象(此时会将注解中的参数传递给 Person类)
第五步:Person中接收 getPerson 函数传递的值
方法的使用
上面的案例是对参数值的使用,下面学习对方法的使用,改造上面的案例
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_FUNCTION)]
class Person
{
public string $name;
public int $age; public function __construct(string $name, int $age)
{
$this->name = $name;
$this->age = $age;
} public function say()
{
echo $this->name, $this->age;
}
} #[Person('api/user/1', 10)]
function getPerson() {} $ref = new ReflectionFunction('getPerson');
$attr = $ref->getAttributes()[0]; // 返回值是一个对象,且对象是Person
$obj = $attr->newInstance();
var_dump($obj);
// 方法调用
$obj->say();
PHP8.2
PHP8新特性的更多相关文章
- php8.0正式版新特性和性能优化学习
前言 PHP团队宣布PHP8正式GA(链接).php的发展又开启了新的篇章,PHP8.0.0版本引入了一些重大变更及许多新特性和性能优化机制.火速学习下~ JIT(Just in Time Compi ...
- SQL Server 2014 新特性——内存数据库
SQL Server 2014 新特性——内存数据库 目录 SQL Server 2014 新特性——内存数据库 简介: 设计目的和原因: 专业名词 In-Memory OLTP不同之处 内存优化表 ...
- ElasticSearch 5学习(10)——结构化查询(包括新特性)
之前我们所有的查询都属于命令行查询,但是不利于复杂的查询,而且一般在项目开发中不使用命令行查询方式,只有在调试测试时使用简单命令行查询,但是,如果想要善用搜索,我们必须使用请求体查询(request ...
- [干货来袭]C#6.0新特性
微软昨天发布了新的VS 2015 ..随之而来的还有很多很多东西... .NET新版本 ASP.NET新版本...等等..太多..实在没消化.. 分享一下也是昨天发布的新的C#6.0的部分新特性吧.. ...
- CSS3新特性应用之结构与布局
一.自适应内部元素 利用width的新特性min-content实现 width新特性值介绍: fill-available,自动填充盒子模型中剩余的宽度,包含margin.padding.borde ...
- 【译】Meteor 新手教程:在排行榜上添加新特性
原文:http://danneu.com/posts/6-meteor-tutorial-for-fellow-noobs-adding-features-to-the-leaderboard-dem ...
- 跨平台的 .NET 运行环境 Mono 3.2 新特性
Mono 3.2 发布了,对 Mono 3.0 和 2.10 版本的支持不再继续,而且这两个分支也不再提供 bug 修复更新. Mono 3.2 主要新特性: LLVM 更新到 3.2 版本,带来更多 ...
- Atitit opencv版本新特性attilax总结
Atitit opencv版本新特性attilax总结 1.1. :OpenCV 3.0 发布,史上功能最全,速度最快的版1 1.2. 应用领域2 1.3. OPENCV2.4.3改进 2.4.2就有 ...
- es6 新特性2
es6其他几个非常有用的新特性. import export 这两个家伙对应的就是es6自己的module功能. 我们之前写的Javascript一直都没有模块化的体系,无法将一个庞大的js工程拆分成 ...
- ES6 新特性
ECMAScript 6(以下简称ES6)是JavaScript语言的下一代标准.因为当前版本的ES6是在2015年发布的,所以又称ECMAScript 2015. 也就是说,ES6就是ES2015. ...
随机推荐
- linux 环境中cat命令进行关键字搜索
在linux环境中通过关键字搜索文件里面的内容 1.显示文件里匹配关键字那行以及上下50行 cat 文件名 | grep -C 50 '关键字' 2.显示关键字及前50行 cat 文件名 | grep ...
- Figma 学习笔记 – Color
大纲 Figma 的颜色是通过 FIll 实现的 (Fill 还有其它功能比如 fill 图片) 整体大概长这样, 我们一个一个看 颜色和 opacity
- Figma 学习笔记 – Team Library Style and Component
Design System 我们做设计通常会 Follow 一个 Design System, 比如 Material Guide. 里头会定义 Font, Color, Effect (Elevat ...
- 2024.09.18初赛模拟MX-S/P6029记录
MX-S 太简单了,没啥难度.\yiw $ 1, 3, 5, 7, 9 $ 的二叉搜索树棵数是卡特兰数. P6029 题意 给定一张有 $ n $ 个点,$ m $ 条边的图.可以任意交换途中两条边的 ...
- 一个.NET开源、快速、低延迟的异步套接字服务器和客户端库
前言 最近有不少小伙伴在问:.NET有什么值得推荐的网络通信框架?今天大姚给大家分享一个.NET开源.免费(MIT License).快速.低延迟的异步套接字服务器和客户端库:NetCoreServe ...
- 46.使用过vuex和vue-router吗
使用过,vuex是状态管理工具,它的数据可以被所有的组件获取,方法可以被所有的组件调用 : vuex 的内部的运行机制:state提供了数据驱动视图,dispath派发actions 执行异步操作, ...
- Nuxt.js 应用中的 kit:compatibility 事件钩子详解
title: Nuxt.js 应用中的 kit:compatibility 事件钩子详解 date: 2024/10/11 updated: 2024/10/11 author: cmdragon e ...
- 云原生周刊:CNCF 2023 年度调查报告 | 2024.4.15
开源项目推荐 highlight 该项目是一个开源全栈监控平台.其功能包括错误监控.会话重放.日志记录.分布式跟踪等. Helm Compose Helm Compose 是一个 helm 插件,用于 ...
- 在 Kubernetes 中基于 StatefulSet 部署 MySQL(上)
大家好,我是老 Z! 本文实现了 MySQL 数据库在基于 KubeSphere 部署的 K8s 集群上的安装部署,部署方式采用了图形化这种形式.下一篇文章将会涉及 GitOps 的基础操作,部署过程 ...
- VS2019插件更新慢的解决办法
VS2019更新巨慢,可以尝试通过以下几个方法解决: 1. 关闭IPV6 关闭IPV6:Win10怎么样禁用IPV6 如何关闭IPV6协议-百度经验 (baidu.com) 2. 选择最快的DNS 百 ...