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 相关文章推荐
基于HTTP长连接的"服务器推"技术的php 简易聊天室
Oct 31 PHP
对象失去焦点时自己动提交数据的实现代码
Nov 06 PHP
php变量作用域的深入解析
Jun 03 PHP
PHP处理大量表单字段的便捷方法
Feb 07 PHP
PHP获取某个月最大天数(最后一天)的方法
Jul 29 PHP
php短信接口代码
May 13 PHP
php使用get_class_methods()函数获取分类的方法
Jul 20 PHP
php array_walk 对数组中的每个元素应用用户自定义函数详解
Nov 18 PHP
PHP实现深度优先搜索算法(DFS,Depth First Search)详解
Sep 16 PHP
php单元测试phpunit入门实例教程
Nov 17 PHP
php DES加密算法实例分析
Sep 18 PHP
thinkphp框架类库扩展操作示例
Nov 26 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
15个小时----从修改程序到自己些程序
2006/10/09 PHP
PHP简单实现断点续传下载的方法
2015/09/25 PHP
使用php+swoole对client数据实时更新(一)
2016/01/07 PHP
一些相见恨晚的 JavaScript 技巧
2010/04/25 Javascript
juqery 学习之四 筛选过滤
2010/11/30 Javascript
基于JavaScript 类的使用详解
2013/05/07 Javascript
JavaScript实现数据类型的相互转换
2016/03/06 Javascript
【经验总结】编写JavaScript代码时应遵循的14条规律
2016/06/20 Javascript
网页挂马方式整理及详细介绍
2016/11/03 Javascript
javascript使用递归算法求两个数字组合功能示例
2017/01/03 Javascript
jquery.cookie.js的介绍与使用方法
2017/02/09 Javascript
vue 动态修改a标签的样式的方法
2018/01/18 Javascript
vue组件挂载到全局方法的示例代码
2018/08/02 Javascript
JavaScript中七种流行的开源机器学习框架
2018/10/11 Javascript
node.JS的crypto加密模块使用方法详解(MD5,AES,Hmac,Diffie-Hellman加密)
2020/02/06 Javascript
[14:19]2018年度COSER大赛-完美盛典
2018/12/16 DOTA
Python获取Windows或Linux主机名称通用函数分享
2014/11/22 Python
python中类和实例如何绑定属性与方法示例详解
2017/08/18 Python
python pygame实现五子棋小游戏
2020/10/26 Python
python3获取当前目录的实现方法
2019/07/29 Python
Pytorch 神经网络—自定义数据集上实现教程
2020/01/07 Python
Python chardet库识别编码原理解析
2020/02/18 Python
在pytorch中动态调整优化器的学习率方式
2020/06/24 Python
CSS3实现类似翻书效果的过渡动画的示例代码
2019/09/06 HTML / CSS
浅谈pc和移动端的响应式的使用
2019/01/03 HTML / CSS
英国家喻户晓的折扣商场:TK Maxx
2017/05/26 全球购物
Tripadvisor新西兰:阅读评论,比较价格和酒店预订
2018/02/10 全球购物
澳大利亚墨水站Ink Station:墨水和碳粉打印机墨盒
2019/03/24 全球购物
临床护士自荐信
2014/01/31 职场文书
石油工程专业毕业生求职信
2014/04/13 职场文书
2014年民政工作总结
2014/11/26 职场文书
2014年妇女工作总结
2014/12/06 职场文书
小学生班干部竞选稿
2015/11/20 职场文书
培训心得体会怎么写
2016/01/25 职场文书
竞选稿之小学班干部
2019/10/31 职场文书
Python爬虫:从m3u8文件里提取小视频的正确操作
2021/05/14 Python