美文网首页PHP
PHP8新特性

PHP8新特性

作者: taobao | 来源:发表于2020-11-27 11:34 被阅读0次

1、PHP8如约而至

访问 PHP官方,我们已经可以看到php8稳定版已经可以下载使用了,这对PHP来说是一个重大版本。有别于PHP7,万众瞩目的Just In Time Compilation(即时编译)功能成为了大家期待的重点。
php8下载地址:
php8新特性介绍:

2、PHP8新特性

  1. 命名参数
    php8支持命名参数,这样靠前的有默认的参数,就不用必须明确写出,如下php8跳过第二和第三个默认参数,直接指定第四个参数。开发者可以按自己的意愿更改参数顺序,这样的好处是,摆脱了php函数有些不经常用的参数靠前,代码中又必须明确写出才能给后面的参数赋值的困扰。
    简单例子如下:详情参考
//php7
htmlspecialchars($string, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);

//php8
htmlspecialchars($string, double_encode: false);
  1. 注解语法
    通过重复使用现有标记T_SL和T_SR,注解是用“ <<”和“ >>”括起来的特殊格式的文本。详情参考

注解可以用该语言应用于许多事物:
函数(包括闭包和短闭包)
类(包括匿名类),接口,特征
类常量
类属性
类方法
功能/方法参数

namespace My\Attributes {
 
    <<PhpAttribute>>
    class SingleArgument {
        public $argumentValue;
 
        public function __construct($argumentValue) {
             $this->argumentValue = $argumentValue;
        }
    }
}
 
namespace {
    <<SingleArgument("Hello World")>>
    class Foo {
    }
 
    $reflectionClass = new \ReflectionClass(Foo::class);
    $attributes = $reflectionClass->getAttributes();
 
    var_dump($attributes[0]->getName());
    var_dump($attributes[0]->getArguments());
    var_dump($attributes[0]->newInstance());
}
 
/**
string(28) "My\Attributes\SingleArgument"
array(1) {
  [0]=>
  string(11) "Hello World"
}
object(My\Attributes\SingleArgument)#1 (1) {
  ["argumentValue"]=>
  string(11) "Hello World"
}
**/
  1. 构造函数参数改进,详情参考
//php7
class Point {
  public float $x;
  public float $y;
  public float $z;

  public function __construct(
    float $x = 0.0,
    float $y = 0.0,
    float $z = 0.0,
  ) {
    $this->x = $x;
    $this->y = $y;
    $this->z = $z;
  }
}

//php8
class Point {
  public function __construct(
    public float $x = 0.0,
    public float $y = 0.0,
    public float $z = 0.0,
  ) {}
}
  1. 联合类型
    当给函数传参,参数可能有多重类型,传统PHP7下并不支持校验,PHP8可以完美实现校验。详情参考
//php7
class Number {
  /** @var int|float */
  private $number;

  /**
   * @param float|int $number
   */
  public function __construct($number) {
    $this->number = $number;
  }
}

new Number('NaN'); // Ok

//php8
class Number {
  public function __construct(
    private int|float $number
  ) {}
}

new Number('NaN'); // TypeError
  1. 匹配表达,详情参考
    php8拥有更精简的新语法,这个赞成的成员非常多,反对的很少,可见大家对switch语法意见颇大。
//php7
switch (8.0) {
  case '8.0':
    $result = "Oh no!";
    break;
  case 8.0:
    $result = "This is what I expected";
    break;
}
echo $result;

//php8
echo match (8.0) {
  '8.0' => "Oh no!",
  8.0 => "This is what I expected",
};
  1. 空安全运算符,详情参考
//php7
$country =  null;

if ($session !== null) {
  $user = $session->user;

  if ($user !== null) {
    $address = $user->getAddress();
 
    if ($address !== null) {
      $country = $address->country;
    }
  }
}

//php8
$country = $session?->user?->getAddress()?->country;
  1. 字符串和数字比较,详情参考
    在php8中,数字和字符串比较时,会将数字转成字符串,正好和之前相反。
//php7
0 == 'foobar' // true

//php8
0 == 'foobar' // false
  1. 函数内部一致性校验错误,详情参考
    php8如果参数验证失败,大多数内部函数将引发Error异常。
    这个改进对开发者提出了更高的要求,原来的warning错误,会直接升级为error错误。
//php7
strlen([]); // Warning: strlen() expects parameter 1 to be string, array given
array_chunk([], -1); // Warning: array_chunk(): Size parameter expected to be greater than 0

//php8
strlen([]); // TypeError: strlen(): Argument #1 ($str) must be of type string, array given
array_chunk([], -1); // ValueError: array_chunk(): Argument #2 ($length) must be greater than 0
  1. 中重要的即时编译功能
    PHP 8引入了两个JIT编译引擎。 跟踪JIT是两者中最有希望的,它在综合基准测试中的性能提高了大约3倍,在某些特定的长期运行的应用程序中提高了1.5–2倍。 典型的应用程序性能与PHP 7.4相当。


    image.png

相关文章

  • PHP8新特性

    1、PHP8如约而至 访问 PHP官方[https://www.php.net/],我们已经可以看到php8稳定版...

  • PHP8新特性

    PHP8包含很多新功能与优化项 包括命名参数、联合类型、注解、构造器属性提升、match 表达式、nullsafe...

  • PHP8 - 纤程的使用

    概述 php8 (php8 >= 8.1.0 ) 中新增了纤程特性,官方文档地址如下: https://www.p...

  • PHP8新特性个人梳理

    2020年11月26日,PHP官方发布了最新的主版本:PHP8,截止当前,其已经更新到了8.0.3。作为phper...

  • PHP8新特性之match

    match类似switch 下面是switch 下面是match

  • PHP 8 新特性之 Attributes (注解)

    PHP8 的 Alpha 版本,过几天就要发布了,其中包含了不少的新特性,当然我自己认为最重要的还是 JIT,这个...

  • PHP8新特性介绍之JIT

    PHP8 alpha1已经在昨天发布,相信关于JIT是大家最关心的,它到底怎么用,有什么要注意的,以及性能提升到底...

  • 【待完成】php8的新特性

    https://www.jianshu.com/p/708802f0f239[https://www.jiansh...

  • PHP8 - 注解的使用

    概述 php8 中新增了注解特性,官方文档地址如下: https://www.php.net/manual/zh/...

  • 解决修改PHP代码之后,刷新没有即时生效的问题

    最近下载PHP8.0来测试,发觉加了断点,改了代码,刷新页面没有任何反应,一开始以为是PHP8新特性,会常驻内存,...

网友评论

    本文标题:PHP8新特性

    本文链接:https://www.haomeiwen.com/subject/hkotwktx.html