您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)PHP7.1和7.2 新增功能有哪些的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過來看看吧。
之前給大家介紹過php7.0的新增功能詳解,今天看下php7.1和php7.2的新功能。
php7.1 新增功能
1.可為空(Nullable)類型
參數(shù)和返回值的類型聲明可以通過在類型名稱前添加一個(gè)問號(hào)(?)來標(biāo)記為空(null)。表明函數(shù)參數(shù)或者返回值的類型要么為指定類型,要么為 null。
看下例子:
function testReturn(?string $name) { return $name; } var_dump(testReturn('yangyi')); var_dump(testReturn(null)); var_dump(testReturn2());
打印輸出:
$ php php71.php string(6) "yangyi" NULL PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function testReturn(), 0 passed in php71.php on line 22 and exactly 1 expected in php71.php:14 Stack trace: #0 php71.php(22): testReturn() #1 {main} thrown in php71.php on line 14
如上如:第三個(gè)報(bào)了一個(gè)致命的錯(cuò)誤。
再來看下,函數(shù)返回值是Nullable的情況:
function testReturn3() : ?string { //return "abc"; //return null; } var_dump(testReturn3());
如果加了? 要么返回 string ,要么返回null。不能啥也不返還。會(huì)報(bào)錯(cuò)。
2.void返回類型
PHP7.0 添加了指定函數(shù)返回類型的特性,但是返回類型卻不能指定為 void,7.1 的這個(gè)特性算是一個(gè)補(bǔ)充。定義返回類型為 void 的函數(shù)不能有返回值,即使返回 null 也不行:
function testReturn4() : void { //1. 要么啥都不返還 ok //2. 要么只return; ok //return; //3. return null 也會(huì)報(bào)錯(cuò) //return null; //4. return 4 會(huì)報(bào)錯(cuò) //return 4; } Fatal error: A void function must not return a value in /php71.php on line 70
還有就是,void 只能用于返回值,不能用于參數(shù)中。比如下面的會(huì)報(bào)錯(cuò):
function testReturn6(void $a) : void { } var_dump(testReturn6()); PHP Fatal error: void cannot be used as a parameter type in php71.php on line 73
如果在類的繼承中,申明為void返回類型的方法,子類要是繼承重寫,也只能返回void, 否則會(huì)觸發(fā)錯(cuò)誤:
<?php class Foo { public function bar(): void { } } class Foobar extends Foo { // 覆蓋失敗 public function bar(): array { // Fatal error: Declaration of Foobar::bar() must be compatible with Foo::bar(): void } }
所以,你必須這樣,就不會(huì)報(bào)錯(cuò):
class Foo { public $a; public function bar(): void { $this->a = 2; } } class Foobar extends Foo { // 覆蓋成功 public function bar(): void { $this->a = 3; } }
3.list 的方括號(hào)([])簡寫以及增加指定key
可以用list 來快速遍歷得到數(shù)組里面的值。現(xiàn)在可以用[]簡寫了。
$data = [ [1, 'Tom'], [2, 'Fred'], ]; // list() style list($id1, $name1) = $data[0]; // [] style [$id1, $name1] = $data[0]; // list() style foreach ($data as list($id, $name)) { // logic here with $id and $name } // [] style foreach ($data as [$id, $name]) { // logic here with $id and $name }
此外,此次更新的list,針對(duì)索引數(shù)組,還可以指定 key,這個(gè)升級(jí)非常棒,非常方便。
$data = [ ["id" => 1, "name" => 'Tom'], ["id" => 2, "name" => 'Fred'], ]; // list() style list("id" => $id1, "name" => $name1) = $data[0]; // [] style ["id" => $id1, "name" => $name1] = $data[0]; // list() style foreach ($data as list("id" => $id, "name" => $name)) { // logic here with $id and $name } // [] style foreach ($data as ["id" => $id, "name" => $name]) { // logic here with $id and $name }
在這個(gè)功能沒有之前,我們一般會(huì)用while + each 的方式來用list 遍歷索引數(shù)組:
$data = [ ["id" => 1, "name" => 'Tom'], ["id" => 2, "name" => 'Fred'], ]; while (list($id, name) = each($data)) { echo "$key => $val\n"; }
注意:PHP 7.2 中已經(jīng)將 each 函數(shù)移除了!所以,就不要用這種方式來遍歷索引數(shù)組了
4.類常量可見范圍設(shè)定
之前類里面額常量用const申明,是沒有可見屬性的。現(xiàn)在把方法的可見屬性移植了過來:
<?php class ConstDemo { // 常量默認(rèn)為 public const PUBLIC_CONST = 0; // 可以自定義常量的可見范圍 public const PUBLIC_CONST_B = 2; protected const PROTECTED_CONST = 3; private const PRIVATE_CONST = 4; // 多個(gè)常量同時(shí)聲明只能有一個(gè)屬性 private const FOO = 1, BAR = 2; }
使用方法和類的方法一樣。就不多詳述了。
5.支持負(fù)的字符串偏移
有2個(gè)更新,1是字符串直接取,2是strpos函數(shù)第三個(gè)參數(shù)支持負(fù)數(shù)。表示從尾部取。
var_dump("abcdef"[-2]); // e var_dump(strpos("aabbcc", "b", -3)); //3
string變量可以直接取值,不用通過變量名,是在php5.5加入的?,F(xiàn)在可以從尾部?。?/p>
var_dump("abcdef"[-2]); // 從末尾取倒數(shù)第2個(gè)字符:e var_dump("abcdef"[2]); // 從前面取第2個(gè),從0開始:c $string = 'bar'; echo $string[1], $string[-1]; // a r
6.多條件 catch
在以往的 try ... catch 語句中,每個(gè) catch 只能設(shè)定一個(gè)條件判斷:
try { // Some code... } catch (ExceptionType1 $e) { // 處理 ExceptionType1 } catch (ExceptionType2 $e) { // 處理 ExceptionType2 } catch (Exception $e) { // ... }
現(xiàn)在可以多個(gè)一起處理。用"|" 分割。
try { // Some code... } catch (ExceptionType1 | ExceptionType2 $e) { // 對(duì)于 ExceptionType1 和 ExceptionType2 的處理 } catch (Exception $e) { // ... }
php7.2
php 7.2大都是底層的更新,提高性能。沒有太大常用語法層面的更新,這里就略過了。
感謝各位的閱讀!關(guān)于PHP7.1和7.2 新增功能有哪些就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。