Home > Backend Development > PHP8 > body text

Interpretation of new features of PHP8 (development code example demonstration)

咔咔
Release: 2023-02-17 11:48:02
Original
4476 people have browsed it

The main content of this article is to interpret the main new features of PHP8.0

Interpretation of new features of PHP8 (development code example demonstration)## PHP8 new features


  • 1. Union type
  • 2. Matching expression Formula
  • 3. Null safe operator
  • 4. Constructor property promotion
  • ##5 . Annotations
  • 6. Named parameters

  • Preface

    ##PHP8 was officially released on November 26, 2020, another Milestone arrives.

    According to the information on the official website, it has been released to version 8.0.2, but many projects are still at version 5.6. If the old ones are not removed, the new ones will not come. If there is actual need, upgrade!

    Next, Kaka will analyze the new features of PHP8.0.

    1. Download PHP8 for Xiaopi Panel

    Since you want to learn new features, you must download it first.

    • Related recommendations: "

      PhpStudy Installation of PHP8 [Detailed Graphical Explanation]"

    Kaka's local environment has always been used It's phpstudy. Currently, the PHP version is only available up to 7.4.

    Open the PHP official website address

    https://www.php.net/ and select window to download.

    Note: The above is DIY manually, which is a bit troublesome. For this reason, php Chinese network has developed an integrated environment that supports php8 specifically for php Chinese network to learn and use. Tool, download address: //m.sbmmt.com/xiazai/gongju/1532

    ##Download methodInterpretation of new features of PHP8 (development code example demonstration)
    Then click the download circled by Click
    Interpretation of new features of PHP8 (development code example demonstration)
    PHP source code download

    The downloaded source code can be directly placed in the D:\phpstudy_pro\Extensions\php directory of the panel

    Then give the folder a name casually. Kaka is written according to the directory structure of phpstudy

    Interpretation of new features of PHP8 (development code example demonstration)
    PHP directory structure

    Then jump Change the PHP environment of the panel and set it to the PHP8 environment

    Interpretation of new features of PHP8 (development code example demonstration)
    Set the PHP environment

    Visit it!

    Interpretation of new features of PHP8 (development code example demonstration)
    PHP8.0 environment configuration

    2. Partial download of PHP8 will cause 502 solution Solution

    If you have downloaded PHP8 and followed the Kaka process, 502 will be returned when accessing.

    Then don’t worry, this is just a small problem, if you find that PHP cannot run successfully.

    Use cmd to go to the PHP8 directory and execute the command php -v. If the following situation occurs, it means that what Kaka said can be solved. If not, go to Baidu separately!

    The following error occurs because VCRUNTIME140.dll is incompatible with the PHP version

    PHP Warning:  'C:\Windows\SYSTEM32\VCRUNTIME140.dll' 14.0 is not compatible with this PHP build linked with 14.28 in Unknown on line 0
    Copy after login

    It is also very simple to solve this problemhttps://www.yuque.com/u30882/rx39g7/ kns2a2, Kaka has downloaded the software, click to download it directly.

    After downloading, just open the installation directly, then restart the computer, and everything will be fine.

    If you do not use the installation package provided by Kaka, you can also directly visit the official website to download it.

    After entering the official website, scroll to the bottom and there are other tools and frameworks. Click the one you selected to download.

    Interpretation of new features of PHP8 (development code example demonstration)
    Download software

    3. Introduction to features

    In the update iteration of the version, new features will appear, and some old features will also be discarded.

    Next, let’s talk about what new features PHP8.0 brings!

    1. Union type

    Regarding the characteristics of union types, they were actually implemented in PHP7, but at that time It is in the form of annotations.

    Now let’s take a look at the difference between the two. The picture below comes from the PHP official website.

    Interpretation of new features of PHP8 (development code example demonstration)
    Interpretation of new features of PHP8 (development code example demonstration)

    PHP8在设置了参数类型后,如果传入类型与预设类型不符合会直接报错

    Interpretation of new features of PHP8 (development code example demonstration)
    Interpretation of new features of PHP8 (development code example demonstration)
    Interpretation of new features of PHP8 (development code example demonstration)
    Interpretation of new features of PHP8 (development code example demonstration)

    但是PHP7就不同,虽然预设了类型,但是传入不对应的类型也可执行。

    Interpretation of new features of PHP8 (development code example demonstration)
    Interpretation of new features of PHP8 (development code example demonstration)

    此功能可以用来限制参数类型,可以更好的对参数进行过滤。

    2. 匹配表达式

    此项功能类似于PHP7的switch语句。

    1. match匹配单值

    相对于switch隐藏了break

     'kaka',    2 => 'niuiniu',};echo $name;  // niuiniu
    Copy after login

    2. 匹配多个条件

     $this->handlePost(),    'get','put' =>  $this->handleGet(),};
    Copy after login

    3. 默认值

    存在跟switch相同的属性default

     'kaka',    2 => 'niuniu',    default => 'heihei',};echo $name;  // heihei
    Copy after login

    4. 如果不设置默认值会报错

    如果不设置default则会报错

     'kaka',    2 => 'niuniu',};echo $name;  // Uncaught UnhandledMatchError: Unhandled match value of type int
    Copy after login

    5. 强制类型匹配

    默认强制类型匹配,如下代码匹配值为int,但是搜索值为字符串3,所以会直接走default

     'kaka',    2 => 'niuniu',    "3" => 'niuniu',    default => 'zero',};echo $name;  // zero
    Copy after login

    3. null安全运算符

    这个特性会非常高效的解决代码的冗余。

    在PHP7中,有时会存在类属性多条件的判断,如下代码

    user = $this;        $this->country='yes';    }    public function getAddress(){        return $this;    }}$session=new Person();if($session!==null){    $user = $session->user;    if($user!==null){        $address = $user->getAddress();        if($address!=null){            $country = $address->country;            if($country!==null){                var_dump($country);            }        }    }}
    Copy after login

    以上代码返回结果为string(3) "yes"

    但是在PHP8中就完美的解决了这种代码冗余的问题

    Interpretation of new features of PHP8 (development code example demonstration)
    Interpretation of new features of PHP8 (development code example demonstration)

    代码

    user = $this;        $this->country='yes';    }    public function getAddress(){        return $this;    }}$session=new Person();echo $session?->user?->getAddress()?->country;
    Copy after login

    同样返回结果也是string(3) "yes"

    可以看到在PHP8中用一行代码即可实现PHP7的7行代码,是不是很nice。

    4. 构造函数属性提升

    PHP7构造函数代码

    kaka = $kaka;    }}
    Copy after login

    PHP8构造函数代码

    kaka;    }}
    Copy after login

    5. 注解

    新增的这个注解特性,咔咔在写了这几个特性之后唯独感觉这个用处不是很大,估计还是很菜的原因。

    但是为了文章的完整性还是写出来给给你们看一下。

    直接上代码了,PHP7获取代码的注释就是用下文代码进行获取的。

    getMethod('show')->getDocComment();$res=substr($doc, strpos($doc, "@api") + strlen("@api "),-2);var_dump($res); // string(32) "http://www.kaka.com/api "
    Copy after login

    在上边代码中使用了好几个字符串的操作,假设注释写的不规范,出错的概率不亚于你写代码少个分号。

    既然有这样的问题,那么官方就给咱们解决了这个问题,接下来看一下在PHP8中是怎么写的。

    getAttributes("api")[0];$name=$attr->getName();$value=$attr->getArguments();var_dump($value[0]);// string(24) "http://www.kaka1.com/api"
    Copy after login

    关于类的注解这里就不说了,有兴趣的可以去官网查阅相关资料。

    6. 命名参数

    最后一点关于PHP8命名参数

    在PHP7使用的都是位置参数,例如如下代码

    也就说你传入的参数是什么在方法接收的地方就对应的是什么。

    paramTest('咔咔',24); // string(8) "咔咔24"
    Copy after login

    那么在PHP8中新增了一项特性就是命名参数,如下代码

    跟上述代码不同的是在方法传参时给每个参数都起了个名字,但是这个名字只能是没有了$ 的参数。

    这个新特性在代码的维护性来看没有一点的优势,咱也不知道设计这个出来干嘛!

    paramTest(age: '咔咔',name: 24);
    Copy after login

    但是在参数这块还是有值得点赞的功能,那就是可变参数,类似于Go的切片

    但是在这里一定要注意的一件事情就是,如果使用了可变参数,那么在传参的时候就不能使用位置参数,而需全部使用命名参数。

     int(1) ["like"]=> string(6) "篮球" }    }}$user = new User();$user->paramTest(age: '咔咔',name: 24,sex:1,like:"篮球");
    Copy after login

    四、总结

    以上就是关于Interpretation of new features of PHP8 (development code example demonstration)咔咔总结的几个常用点。

    PHP8也添加了新的功能和类库、错误处理、字符串处理、面向对象编程的更改等。

    当然在关注新特性的同时还是需要关注废弃的东西,在PHP8中废弃最多的都是关于反射的几个方法。

    以上就是咔咔总结的PHP8更新的主要特性,没有总结的特别全面,只是把在开发过程中能使用到的写了出来。

    Interpretation of new features of PHP8 (development code example demonstration)

    (Original first release on the public account, welcome to follow!)

    Keep learning, keep blogging, and keep sharing is Kaka The belief that I have always upheld since I started working in the industry. I hope that Kaka’s articles on the huge Internet can bring you a little bit of help. I am Kaka, see you in the next issue.

The above is the detailed content of Interpretation of new features of PHP8 (development code example demonstration). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!