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 相关文章推荐
使用数据库保存session的方法
Oct 09 PHP
PHP入门速成教程
Mar 19 PHP
PHP中改变图片的尺寸大小的代码
Jul 17 PHP
微信扫描二维码登录网站代码示例
Dec 30 PHP
一个不易被发现的PHP后门代码解析
Jul 05 PHP
php使用fgetcsv读取csv文件出现乱码的解决方法
Nov 08 PHP
提高php编程效率技巧
Aug 13 PHP
PHP微信开发之有道翻译
Jun 23 PHP
windows server 2008/2012安装php iis7 mysql环境搭建教程
Jun 30 PHP
Yii2中cookie用法示例分析
Jul 18 PHP
php微信公众平台开发(一) 配置接口
Dec 06 PHP
php获取是星期几的的一些常用姿势
Dec 15 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维护文件系统
2006/10/09 PHP
PHP中一个控制字符串输出的函数
2006/10/09 PHP
如何使用PHP往windows中添加用户
2006/12/06 PHP
IP138 IP地址查询小偷实现代码
2010/02/15 PHP
Yii2 rbac权限控制之菜单menu实例教程
2016/04/28 PHP
ThinkPHP中create()方法自动验证表单信息
2017/04/28 PHP
php获取是星期几的的一些常用姿势
2019/12/15 PHP
jQuery学习总结之元素的相对定位和选择器(持续更新)
2011/04/26 Javascript
.net,js捕捉文本框回车键事件的小例子(兼容多浏览器)
2013/03/11 Javascript
JavaScript截取字符串的Slice、Substring、Substr函数详解和比较
2014/03/20 Javascript
Area 区域实现post提交数据的js写法
2014/04/22 Javascript
js判断浏览器类型为ie6时不执行
2014/06/15 Javascript
jQuery调取jSon数据并展示的方法
2015/01/29 Javascript
javascript动态添加删除tabs标签的方法
2015/07/06 Javascript
微信小程序(应用号)开发新闻客户端实例
2016/10/24 Javascript
bootstrap为水平排列的表单和内联表单设置可选的图标
2017/02/15 Javascript
webpack配置sass模块的加载的方法
2017/07/30 Javascript
jQuery Ajax向服务端传递数组参数值的实例代码
2017/09/03 jQuery
详解如何配置vue-cli3.0的vue.config.js
2018/08/23 Javascript
JS自定义对象创建与简单使用方法示例
2020/01/15 Javascript
[56:48]FNATIC vs EG 2019国际邀请赛小组赛 BO2 第二场 8.15
2019/08/16 DOTA
Python中的defaultdict模块和namedtuple模块的简单入门指南
2015/04/01 Python
详解Python中的正则表达式的用法
2015/04/09 Python
python使用multiprocessing模块实现带回调函数的异步调用方法
2015/04/18 Python
解决tensorflow测试模型时NotFoundError错误的问题
2018/07/27 Python
使用django和vue进行数据交互的方法步骤
2019/11/11 Python
python如何安装下载后的模块
2020/07/03 Python
Django crontab定时任务模块操作方法解析
2020/09/10 Python
The North Face北面英国官网:美国著名户外品牌
2017/12/13 全球购物
巴西最大的巴士票务门户:Quero Passagem
2020/11/21 全球购物
linux面试题参考答案(10)
2013/11/04 面试题
2015最新婚礼主持词
2015/06/30 职场文书
2016年小学中秋节活动总结
2016/04/05 职场文书
Mysql效率优化定位较低sql的两种方式
2021/05/26 MySQL
openGauss数据库JDBC环境连接配置的详细过程(Eclipse)
2022/06/01 Java/Android