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 相关文章推荐
使用 php4 加速 web 传输
Oct 09 PHP
php实现从ftp服务器上下载文件树到本地电脑的程序
Feb 10 PHP
超级好用的一个php上传图片类(随机名,缩略图,加水印)
Jun 30 PHP
php中echo()和print()、require()和include()等易混淆函数的区别
Feb 22 PHP
php环境无法上传文件的解决方法
Apr 30 PHP
PHP中怎样防止SQL注入分析
Oct 23 PHP
php中convert_uuencode()与convert_uuencode函数用法实例
Nov 22 PHP
PHP输出图像imagegif、imagejpeg与imagepng函数用法分析
Nov 14 PHP
iOS+PHP注册登录系统 PHP部分(上)
Dec 26 PHP
laravel框架之数据库查出来的对象实现转化为数组
Oct 23 PHP
php设计模式之建造器模式分析【星际争霸游戏案例】
Jan 23 PHP
php7 错误处理机制修改实例分析
May 25 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替换超长文本中的特殊字符的函数代码
2012/05/22 PHP
深入apache host的配置详解
2013/06/09 PHP
php 检查电子邮件函数(自写)
2014/01/16 PHP
php显示时间常用方法小结
2015/06/05 PHP
thinkPHP多域名情况下使用memcache方式共享session数据的实现方法
2016/07/21 PHP
thinkPHP5.0框架配置格式、加载解析与读取方法
2017/03/17 PHP
Thinkphp框架使用list_to_tree 实现无限级分类列出所有节点示例
2020/04/04 PHP
js资料prototype 属性
2007/03/13 Javascript
一句话JavaScript表单验证代码
2009/08/02 Javascript
实现连缀调用的map方法(prototype)
2009/08/05 Javascript
jQuery 瀑布流 绝对定位布局(二)(延迟AJAX加载图片)
2012/05/23 Javascript
JQuery事件e参数的方法preventDefault()取消默认行为
2013/09/26 Javascript
js带点自动图片轮播幻灯片特效代码分享
2015/09/07 Javascript
jQuery实现产品对比功能附源码下载
2016/08/09 Javascript
JavaScript实现图像模糊化的方法实例
2017/01/15 Javascript
浅析vue 函数配置项watch及函数 $watch 源码分享
2018/11/22 Javascript
vue基础之data存储数据及v-for循环用法示例
2019/03/08 Javascript
JS array数组检测方式解析
2020/05/19 Javascript
详解javascript脚本何时会被执行
2021/02/05 Javascript
[35:44]2014 DOTA2华西杯精英邀请赛 5 24 iG VS VG
2014/05/26 DOTA
python实现的自动发送消息功能详解
2019/08/15 Python
Python Django 封装分页成通用的模块详解
2019/08/21 Python
Pycharm添加虚拟解释器报错问题解决方案
2020/10/13 Python
html5拍照功能实现代码(htm5上传文件)
2013/12/11 HTML / CSS
应届毕业生求职信范文分享
2013/12/26 职场文书
数学教学随笔感言
2014/02/17 职场文书
党员违纪检讨书
2014/02/18 职场文书
酒店管理求职信范文
2014/04/06 职场文书
数字化校园建设方案
2014/05/03 职场文书
超越自我演讲稿
2014/05/21 职场文书
个人作风建设剖析材料
2014/10/11 职场文书
2014大学班主任工作总结
2014/11/08 职场文书
合同权益转让协议书模板
2014/11/18 职场文书
小学班主任评语
2014/12/29 职场文书
2016年“六一儿童节”校园广播稿
2015/12/17 职场文书
个人工作总结(管理人员)范文
2019/08/13 职场文书