Home > Article > Backend Development > Interpretation of new features of PHP8 (development code example demonstration)
“The main content of this article is to interpret the main new features of PHP8.0
”
PhpStudy Installation of PHP8 [Detailed Graphical Explanation]"
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
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
Then jump Change the PHP environment of the panel and set it to the PHP8 environment
Visit it!
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
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.
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!
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.
PHP8在设置了参数类型后,如果传入类型与预设类型不符合会直接报错
但是PHP7就不同,虽然预设了类型,但是传入不对应的类型也可执行。
此功能可以用来限制参数类型,可以更好的对参数进行过滤。
此项功能类似于PHP7的switch语句。
1. match匹配单值
相对于switch隐藏了break
<?php$name = match(2) { 1 => 'kaka', 2 => 'niuiniu',};echo $name; // niuiniu
2. 匹配多个条件
<?php$method = $_SERVER['REQUEST_METHOD'];match($method) { 'post' => $this->handlePost(), 'get','put' => $this->handleGet(),};
3. 默认值
存在跟switch相同的属性default
<?php$name = match(3) { 1 => 'kaka', 2 => 'niuniu', default => 'heihei',};echo $name; // heihei
4. 如果不设置默认值会报错
如果不设置default则会报错
<?php$name = match(3) { 1 => 'kaka', 2 => 'niuniu',};echo $name; // Uncaught UnhandledMatchError: Unhandled match value of type int
5. 强制类型匹配
默认强制类型匹配,如下代码匹配值为int,但是搜索值为字符串3,所以会直接走default
<?php$name = match(3) { 1 => 'kaka', 2 => 'niuniu', "3" => 'niuniu', default => 'zero',};echo $name; // zero
这个特性会非常高效的解决代码的冗余。
在PHP7中,有时会存在类属性多条件的判断,如下代码
<?phpclass Person{ public $user; public $country; public function __construct(){ $this->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); } } }}
以上代码返回结果为string(3) "yes"
但是在PHP8中就完美的解决了这种代码冗余的问题
代码
<?phpclass Person{ public $user; public $country; public function __construct(){ $this->user = $this; $this->country='yes'; } public function getAddress(){ return $this; }}$session=new Person();echo $session?->user?->getAddress()?->country;
同样返回结果也是string(3) "yes"
可以看到在PHP8中用一行代码即可实现PHP7的7行代码,是不是很nice。
PHP7构造函数代码
<?phpclass User { public string $kaka; public function __construct(string $kaka) { $this->kaka = $kaka; }}
PHP8构造函数代码
<?phpclass User { public function __construct(public string $kaka;) { echo $this->kaka; }}
新增的这个注解特性,咔咔在写了这几个特性之后唯独感觉这个用处不是很大,估计还是很菜的原因。
但是为了文章的完整性还是写出来给给你们看一下。
直接上代码了,PHP7获取代码的注释就是用下文代码进行获取的。
<?phpclass User { /** * @api http://www.kaka.com/api */ function show($name){}}$re=new ReflectionClass(new User);$doc = $re->getMethod('show')->getDocComment();$res=substr($doc, strpos($doc, "@api") + strlen("@api "),-2);var_dump($res); // string(32) "http://www.kaka.com/api "
在上边代码中使用了好几个字符串的操作,假设注释写的不规范,出错的概率不亚于你写代码少个分号。
既然有这样的问题,那么官方就给咱们解决了这个问题,接下来看一下在PHP8中是怎么写的。
<?php#[api("http://www.kaka.com/api")]function show($name){}$ref=new ReflectionFunction("show");$attr=$ref->getAttributes("api")[0];$name=$attr->getName();$value=$attr->getArguments();var_dump($value[0]);// string(24) "http://www.kaka1.com/api"
关于类的注解这里就不说了,有兴趣的可以去官网查阅相关资料。
最后一点关于PHP8命名参数
在PHP7使用的都是位置参数,例如如下代码
也就说你传入的参数是什么在方法接收的地方就对应的是什么。
<?phpclass User { public function paramTest ($name,$age) { var_dump($name.$age); }}$user = new User();$user->paramTest('咔咔',24); // string(8) "咔咔24"
那么在PHP8中新增了一项特性就是命名参数,如下代码
跟上述代码不同的是在方法传参时给每个参数都起了个名字,但是这个名字只能是没有了$ 的参数。
这个新特性在代码的维护性来看没有一点的优势,咱也不知道设计这个出来干嘛!
<?phpclass User { public function paramTest ($name,$age) { var_dump($name.$age);// string(8) "24咔咔" }}$user = new User();$user->paramTest(age: '咔咔',name: 24);
但是在参数这块还是有值得点赞的功能,那就是可变参数,类似于Go的切片
但是在这里一定要注意的一件事情就是,如果使用了可变参数,那么在传参的时候就不能使用位置参数,而需全部使用命名参数。
<?phpclass User { public function paramTest ($name,$age,...$other) { var_dump($other); // array(2) { ["sex"]=> int(1) ["like"]=> string(6) "篮球" } }}$user = new User();$user->paramTest(age: '咔咔',name: 24,sex:1,like:"篮球");
以上就是关于Interpretation of new features of PHP8 (development code example demonstration)咔咔总结的几个常用点。
PHP8也添加了新的功能和类库、错误处理、字符串处理、面向对象编程的更改等。
当然在关注新特性的同时还是需要关注废弃的东西,在PHP8中废弃最多的都是关于反射的几个方法。
以上就是咔咔总结的PHP8更新的主要特性,没有总结的特别全面,只是把在开发过程中能使用到的写了出来。
(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!