当前位置: 移动技术网 > IT编程>开发语言>PHP > PHP 8新特性简介

PHP 8新特性简介

2020年08月19日  | 移动技术网IT编程  | 我要评论

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新特性的资料请关注移动技术网其它相关文章!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网