PHP 8新特性简介


Posted in PHP onAugust 18, 2020

PHP 8新特性

新的主要PHP版本PHP 8预计将于2020年底发布。它现在处于非常活跃的开发阶段,所以在接下来的几个月里,事情可能会发生很大的变化。

在这篇文章中,我将持续更新预期的内容列表:新特性、性能改进和重大变化。因为PHP 8是一个新的主版本,所以您的代码被破坏的几率更高。如果你一直在更新最新的版本,升级应该不会太困难,因为大多数有破坏性的更改在7之前就已经废弃了。*版本。

除了中断更改之外,PHP 8还带来了一些不错的新特性,比如JIT编译器和union类型;还有更多!

Union types:联合类型

考虑到PHP的动态类型化特性,在很多情况下联合类型是有用的。联合类型是两个或多个类型的集合,这些类型表示其中一个可以使用。

public function foo(Foo|Bar $input): int|float;

注意,void永远不能是union类型的一部分,因为它表示“根本没有返回值”。此外,可以使用|null来编写可为空的联合,也可以使用现有的?符号:

public function foo(Foo|null $foo): void;

public function bar(?Bar $bar): void;

JIT

即时编译器承诺显著的性能改进,尽管并不总是在web请求的上下文中。目前还没有任何准确的基准,但它们肯定会到来。

Static return type:静态的返回类型

虽然已经可以返回self,但静态类型直到PHP 8才成为有效的返回类型。考虑到PHP的动态类型特性,这一特性对许多开发人员都很有用。

class Foo
{
  public function test(): static
  {
    return new static();
  }
}

Weak maps

在PHP 7.4中添加的weakrefs RFC的基础上,在PHP 8中添加了WeakMap实现。弱映射包含对对象的引用,这并不会阻止那些对象被垃圾收集。

以orm为例,它们通常实现保存对实体类的引用的缓存,以改进实体之间关系的性能。这些实体对象不能被垃圾回收,只要这个缓存有一个对它们的引用,即使缓存是唯一引用它们的东西。

如果这个缓存层使用弱引用和映射,那么PHP将在没有其他对象引用它们时对这些对象进行垃圾收集。尤其是orm,它可以在一个请求中管理数百个(如果不是数千个)实体;弱映射为处理这些对象提供了一种更好的、对资源更友好的方法。

下面是弱映射的样子,一个来自RFC的例子:

class Foo 
{
  private WeakMap $cache;

  public function getSomethingWithCaching(object $obj): object
  {
    return $this->cache[$obj]
      ??= $this->computeSomethingExpensive($obj);
  }
}

::class on objects

一个小而有用的新特性:现在可以在对象上使用::class,而不必在对象上使用get_class()。它的工作方式与get_class()相同。

$foo = new Foo();

var_dump($foo::class);

Stringable interface

Stringable接口可用于键入提示任何字符串或实现了 tostring()的内容。而且,无论何时类实现了 tostring(),它都会在后台自动实现接口,不需要手动实现。

class Foo
{
  public function __toString(): string
  {
    return 'foo';
  }
}

function bar(Stringable $stringable) { /* … */ }

bar(new Foo());
bar('abc');

从接口创建DateTime对象

您已经可以使用DateTime:: createfromimmutabledatetime ($immutableDateTime)从一个datetime对象创建一个DateTime对象,但是另一种方法比较麻烦。通过添加DateTime::createFromInterface()和datetime::createFromInterface(),现在就有了一种将DateTime和datetime对象相互转换的通用方法。

DateTime::createFromInterface(DateTimeInterface $other);

DateTimeImmutable::createFromInterface(DateTimeInterface $other);

重新定义引擎的警告

许多以前只触发警告或通知的错误现在已经转换为正确的错误。以下警告已更改。

  • Undefined variable: Error exception instead of notice
  • Undefined array index: warning instead of notice
  • Division by zero: DivisionByZeroError exception instead of warning
  • Attempt to increment/decrement property ‘%s' of non-object: Error exception instead of warning
  • Attempt to modify property ‘%s' of non-object: Error exception instead of warning
  • Attempt to assign property ‘%s' of non-object: Error exception instead of warning
  • Creating default object from empty value: Error exception instead of warning
  • Trying to get property ‘%s' of non-object: warning instead of notice
  • Undefined property: %s::$%s: warning instead of notice
  • Cannot add element to the array as the next element is already occupied: Error exception instead of warning
  • Cannot unset offset in a non-array variable: Error exception instead of warning
  • Cannot use a scalar value as an array: Error exception instead of warning
  • Only arrays and Traversables can be unpacked: TypeError exception instead of warning
  • Invalid argument supplied for foreach(): TypeError exception instead of warning
  • Illegal offset type: TypeError exception instead of warning
  • Illegal offset type in isset or empty: TypeError exception instead of warning
  • Illegal offset type in unset: TypeError exception instead of warning
  • Array to string conversion: warning instead of notice
  • Resource ID#%d used as offset, casting to integer (%d): warning instead of notice
  • String offset cast occurred: warning instead of notice
  • Uninitialized string offset: %d: warning instead of notice
  • Cannot assign an empty string to a string offset: Error exception instead of warning

以上就是PHP 8新特性简介的详细内容,更多关于php 8新特性的资料请关注三水点靠木其它相关文章!

PHP 相关文章推荐
PHP文件上传原理简单分析
May 29 PHP
php5.2 Json不能正确处理中文、GB编码的解决方法
Mar 28 PHP
PHP根据传来的16进制颜色代码自动改变背景颜色
Jun 13 PHP
ThinkPHP采用GET方式获取中文参数查询无结果的解决方法
Jun 26 PHP
php实现字符串反转输出的方法
Mar 14 PHP
php利用imagemagick实现复古老照片效果实例
Feb 16 PHP
利用PHP实现一个简单的用户登记表示例
Apr 25 PHP
PHP基于简单递归函数求一个数阶乘的方法示例
Apr 26 PHP
php头像上传预览实例代码
May 02 PHP
支持汉转拼和拼音分词的PHP中文工具类ChineseUtil
Feb 23 PHP
php识别翻转iphone拍摄的颠倒图片
May 17 PHP
laravel框架上传图片实现实时预览功能
Oct 14 PHP
PHP大文件及断点续传下载实现代码
Aug 18 #PHP
基于thinkphp5框架实现微信小程序支付 退款 订单查询 退款查询操作
Aug 17 #PHP
PhpStorm2020.1 安装 debug - Postman 调用的详细教程
Aug 17 #PHP
php开发最强大的IDE编辑的phpstorm 2020.2配置Xdebug调试的详细教程
Aug 17 #PHP
PHP unset函数原理及使用方法解析
Aug 14 #PHP
PHP常量及变量区别原理详解
Aug 14 #PHP
PHP获取当前时间不准确问题解决方案
Aug 14 #PHP
You might like
php中的PHP_EOL换行符详细解析
2013/10/26 PHP
PHP遍历目录文件的常用方法小结
2017/02/03 PHP
PHP发送邮件确认验证注册功能示例【修改别人邮件类】
2019/11/09 PHP
自己动手实现jQuery Callbacks完整功能代码详解
2013/11/25 Javascript
jQuery focus和blur事件的应用详解
2014/01/26 Javascript
使用jquery解析XML示例代码
2014/09/05 Javascript
jQuery 判断图片是否加载完成方法汇总
2015/08/10 Javascript
javascript中checkbox使用方法简单实例演示
2015/11/17 Javascript
jQuery增加与删除table列的方法
2016/03/01 Javascript
JS检测移动端横竖屏的代码
2016/05/30 Javascript
JavaScript面试开发常用的知识点总结
2016/08/08 Javascript
node.js实现快速截图
2016/08/27 Javascript
浅谈jQuery中Ajax事件beforesend及各参数含义
2016/12/03 Javascript
Iphone手机、安卓手机浏览器控制默认缩放大小的方法总结(附代码)
2017/08/18 Javascript
javascript算法之二叉搜索树的示例代码
2017/09/12 Javascript
微信小程序封装多张图片上传api代码实例
2019/12/30 Javascript
vue项目中使用多选框的实例代码
2020/07/22 Javascript
[04:00]DOTA2解说界神雕侠侣 CJ第四天谷子现场过生日
2013/07/30 DOTA
Python文件去除注释的方法
2015/05/25 Python
Python实现简单的HttpServer服务器示例
2017/09/25 Python
python实现输入数字的连续加减方法
2018/06/22 Python
Python替换月份为英文缩写的实现方法
2019/07/15 Python
Python 迭代,for...in遍历,迭代原理与应用示例
2019/10/12 Python
详解python的super()的作用和原理
2020/10/29 Python
python解压zip包中文乱码解决方法
2020/11/27 Python
解决python3.6用cx_Oracle库连接Oracle的问题
2020/12/07 Python
写clone()方法时,通常都有一行代码,是什么?
2012/10/31 面试题
学习委员自我鉴定
2014/01/13 职场文书
房屋买卖协议书
2014/04/10 职场文书
共产党员公开承诺践诺书
2014/05/28 职场文书
党在我心中的演讲稿
2014/09/13 职场文书
创先争优个人总结
2015/03/04 职场文书
庆祝教师节新闻稿
2015/07/17 职场文书
2016年小学生新年寄语
2015/08/18 职场文书
如何使用PostgreSQL进行中文全文检索
2021/05/27 PostgreSQL
Jackson 反序列化时实现大小写不敏感设置
2021/06/29 Java/Android