PHP 8新特性简介

 更新时间:2020年08月18日 14:45:04   作者:麦叶  
这篇文章主要介绍了php8新特性的相关资料,帮助大家决定是否要升级新版本,感兴趣的朋友可以了解下

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新特性的资料请关注脚本之家其它相关文章!

相关文章

  • PHP6 先修班 JSON实例代码

    PHP6 先修班 JSON实例代码

    先簡單的談一下什麼是 JSON JSON 全稱 JavaScript Object Notation是一種非常輕量級的資料交換格式
    2008-08-08
  • PHP调用MySQL的存储过程的实现代码

    PHP调用MySQL的存储过程的实现代码

    MySQL好像从5.0开始才引入存储过程,反正以前做应用的时候从没碰过,不过现在因为主要作内部系统
    2008-08-08
  • 详解PHP解决守护进程Redis假死

    详解PHP解决守护进程Redis假死

    公司业务有一个常驻后台运行的守护进程。在这个守护进程当中使用了 Redis List 结构保存业务数据进行队列消费。结果运行过程中,有时候半个月,有时候几个月就会突然不再消费队列里面的数据。我们发现进行心中检测之后,程序的稳定性大大提高。
    2021-06-06
  • PHP中遍历stdclass object的实现代码

    PHP中遍历stdclass object的实现代码

    从网上查到的方法是 用get_object_vars 把类类型转换成数组 然后在用 foreach 遍历即可
    2011-06-06
  • PHP简单实现文本计数器的方法

    PHP简单实现文本计数器的方法

    这篇文章主要介绍了PHP简单实现文本计数器的方法,涉及PHP针对文本文件的简单判断,读取及写入等操作技巧,需要的朋友可以参考下
    2016-04-04
  • 深入理解php的MySQL连接类

    深入理解php的MySQL连接类

    本篇文章是对php的MySQL连接类进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • php 操作excel文件的方法小结

    php 操作excel文件的方法小结

    用php生成excel文件的方法,单独就是用的php,注意一下数据的输入方法即可。
    2009-12-12
  • php的优点总结 php有哪些优点

    php的优点总结 php有哪些优点

    在本篇文章里小编给各位整理了关于php的优点的总结内容以及相关知识点整理,需要的朋友们学习下。
    2019-07-07
  • php判断字符以及字符串的包含方法属性

    php判断字符以及字符串的包含方法属性

    php判断字符以及字符串的包含,可以使用PHP的内置函数strstr,strpos,stristr直接进行判断.也可以通过explode函数的作用写一个判断函数
    2008-08-08
  • PHP Token(令牌)设计

    PHP Token(令牌)设计

    PHP Token(令牌)设计 设计目标: 避免重复提交数据. 检查来路,是否是外部提交 匹配要执行的动作(如果有多个逻辑在同一个页面实现,比如新增,删除,修改放到一个PHP文件里操作) 这里所说的token是在页面显示的时候,写到FORM的一个隐藏表单项(type=hidden). token不可明文,如果是明文,那就太危险了,所以要采用一定的加密方式.密文要可逆.俺算法很白痴,所以采用了网上一个现成的方法.
    2008-03-03

最新评论