PHP 7.1新特性的匯總介紹
一、可空類型
可空類型主要用于參數(shù)類型聲明和函數(shù)返回值聲明。
主要的兩種形式如下:
<?php function answer(): ?int { return null; //ok } function answer(): ?int { return 42; // ok } function say(?string $msg) { if ($msg) { echo $msg; } }
從例子很容易理解,所指的就是通過 ? 的形式表明函數(shù)參數(shù)或者返回值的類型要么為指定類型,要么為 null。
此方法也可用于接口函數(shù)的定義:
<?php interface Fooable { function foo(?Fooable $f); }
但有一個(gè)需要注意的地方:如果函數(shù)本身定義了參數(shù)類型并且沒有默認(rèn)值,即使是可空的,也不能省略,否則會(huì)觸發(fā)錯(cuò)誤。如下:
<?php function foo_nullable(?Bar $bar) {} foo_nullable(new Bar); // 可行 foo_nullable(null); // 可行 foo_nullable(); // 不可行
但是如果以上函數(shù)的參數(shù)定義為 ?Bar $bar = null
的形式,則第三種寫法也是可行的。因?yàn)?= null
實(shí)際上相當(dāng)于 ? 的超集,對(duì)于可空類型的參數(shù),可以設(shè)定 null 為默認(rèn)值。
二、list 的方括號(hào)簡(jiǎn)寫
我們知道在 PHP5.4 之前只能通過 array()
來定義數(shù)組,5.4之后添加了 [] 的簡(jiǎn)化寫法(省略了5個(gè)字符還是很實(shí)在的)。
<?php // 5.4 之前 $array = array(1, 2, 3); $array = array("a" => 1, "b" => 2, "c" => 3); // 5.4 及之后 $array = [1, 2, 3]; $array = ["a" => 1, "b" => 2, "c" => 3];
引申到另外一個(gè)問題上,如果我們要把數(shù)組的值賦值給不同的變量,可以通過 list
來實(shí)現(xiàn):
<?php list($a, $b, $c) = $array;
是否也可以通過 [] 的簡(jiǎn)寫來實(shí)現(xiàn)呢?
<?php [$a, $b, $c] = $array;
以及下一個(gè)特性中會(huì)提到的 list
指定 key
:
<?php ["a" => $a, "b" => $b, "c" => $c] = $array;
PHP7.1 實(shí)現(xiàn)了這個(gè)特性。但是要注意的是:出現(xiàn)在左值中的 [] 并不是數(shù)組的簡(jiǎn)寫,是 list()
的簡(jiǎn)寫。
但是并不僅僅如此,新的 list()
的實(shí)現(xiàn)并不僅僅可以出現(xiàn)在左值中,也能在 foreach
循環(huán)中使用:
<?php foreach ($points as ["x" => $x, "y" => $y]) { var_dump($x, $y); }
不過因?yàn)閷?shí)現(xiàn)的問題,list()
和 [] 不能相互嵌套使用:
<?php // 不合法 list([$a, $b], [$c, $d]) = [[1, 2], [3, 4]]; // 不合法 [list($a, $b), list($c, $d)] = [[1, 2], [3, 4]]; // 合法 [[$a, $b], [$c, $d]] = [[1, 2], [3, 4]];
三、允許在 list 中指定 key
上文提到過,新的 list()
的實(shí)現(xiàn)中可以指定key:
<?php $array = ["a" => 1, "b" => 2, "c" => 3]; ["a" => $a, "b" => $b, "c" => $c] = $array;
這也就相當(dāng)于:
<?php $a = $array['a']; $b = $array['b']; $c = $array['c'];
和以往的區(qū)別在于以往的 list()
的實(shí)現(xiàn)相當(dāng)于 key 只能是 0, 1, 2, 3 的數(shù)字形式并且不能調(diào)整順序。執(zhí)行以下語句:
<?php list($a, $b) = [1 => '1', 2 => '2'];
會(huì)得到 PHP error: Undefined offset: 0...
的錯(cuò)誤。
而新的實(shí)現(xiàn)則可以通過以下方式來調(diào)整賦值:
<?php list(1 => $a, 2 => $b) = [1 => '1', 2 => '2'];
不同于數(shù)組的是,list
并不支持混合形式的 key
,以下寫法會(huì)觸發(fā)解析錯(cuò)誤:
<?php // Parse error: syntax error, ... list($unkeyed, "key" => $keyed) = $array;
更復(fù)雜的情況,list
也支持復(fù)合形式的解析:
<?php $points = [ ["x" => 1, "y" => 2], ["x" => 2, "y" => 1] ]; list(list("x" => $x1, "y" => $y1), list("x" => $x2, "y" => $y2)) = $points; $points = [ "first" => [1, 2], "second" => [2, 1] ]; list("first" => list($x1, $y1), "second" => list($x2, $y2)) = $points;
以及循環(huán)中使用:
<?php $points = [ ["x" => 1, "y" => 2], ["x" => 2, "y" => 1] ]; foreach ($points as list("x" => $x, "y" => $y)) { echo "Point at ($x, $y)", PHP_EOL; }
四、void 返回類型
PHP7.0 添加了指定函數(shù)返回類型的特性,但是返回類型卻不能指定為 void
,7.1 的這個(gè)特性算是一個(gè)補(bǔ)充:
<?php function should_return_nothing(): void { return 1; // Fatal error: A void function must not return a value }
以下兩種情況都可以通過驗(yàn)證:
<?php function lacks_return(): void { // valid } function returns_nothing(): void { return; // valid }
定義返回類型為 void
的函數(shù)不能有返回值,即使返回 null
也不行:
<?php function returns_one(): void { return 1; // Fatal error: A void function must not return a value } function returns_null(): void { return null; // Fatal error: A void function must not return a value }
此外 void
也只適用于返回類型,并不能用于參數(shù)類型聲明,或者會(huì)觸發(fā)錯(cuò)誤:
<?php function foobar(void $foo) { // Fatal error: void cannot be used as a parameter type }
類函數(shù)中對(duì)于返回類型的聲明也不能被子類覆蓋,否則會(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 } }
五、類常量屬性設(shè)定
這個(gè)特性說起來比較簡(jiǎn)單,就是現(xiàn)在類中的常量支持使用 public
、private
和 protected
修飾了:
<?php class Token { // 常量默認(rèn)為 public const PUBLIC_CONST = 0; // 可以自定義常量的可見范圍 private const PRIVATE_CONST = 0; protected const PROTECTED_CONST = 0; public const PUBLIC_CONST_TWO = 0; // 多個(gè)常量同時(shí)聲明只能有一個(gè)屬性 private const FOO = 1, BAR = 2; }
此外,接口(interface
)中的常量只能是 public
屬性:
<?php interface ICache { public const PUBLIC = 0; const IMPLICIT_PUBLIC = 1; }
為了應(yīng)對(duì)變化,反射類的實(shí)現(xiàn)也相應(yīng)的豐富了一下,增加了 getReflectionConstant
和 getReflectionConstants
兩個(gè)方法用于獲取常量的額外屬性:
<?php class testClass { const TEST_CONST = 'test'; } $obj = new ReflectionClass( "testClass" ); $const = $obj->getReflectionConstant( "TEST_CONST" ); $consts = $obj->getReflectionConstants();
六、多條件 catch
在以往的 try ... catch
語句中,每個(gè) catch
只能設(shè)定一個(gè)條件判斷:
<?php try { // Some code... } catch (ExceptionType1 $e) { // 處理 ExceptionType1 } catch (ExceptionType2 $e) { // 處理 ExceptionType2 } catch (\Exception $e) { // ... }
新的實(shí)現(xiàn)中可以在一個(gè) catch
中設(shè)置多個(gè)條件,相當(dāng)于或的形式判斷:
<?php try { // Some code... } catch (ExceptionType1 | ExceptionType2 $e) { // 對(duì)于 ExceptionType1 和 ExceptionType2 的處理 } catch (\Exception $e) { // ... }
對(duì)于異常的處理簡(jiǎn)化了一些。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家學(xué)習(xí)或者使用PHP7.1能有一定的幫助,如果有疑問大家可以留言交流。
附:源 RFC 地址
Nullable Types
Square bracket syntax for array destructuring assignment
Allow specifying keys in list()
Generalize support of negative string offsets
Void Return Type
Class constant visibility modifiers
Multi catch
相關(guān)文章
php實(shí)現(xiàn)獲取近幾日、月時(shí)間示例
這篇文章主要介紹了php實(shí)現(xiàn)獲取近幾日、月時(shí)間的方法,結(jié)合實(shí)例形式分析了PHP日期時(shí)間相關(guān)的date_default_timezone_set時(shí)區(qū)設(shè)置、date與mktime時(shí)間計(jì)算、轉(zhuǎn)換等操作技巧,需要的朋友可以參考下2019-07-07用Zend Studio+PHPnow+Zend Debugger搭建PHP服務(wù)器調(diào)試環(huán)境步驟
作為一名非專業(yè)的PHP開發(fā)者,想要較方便地讀懂并搞清楚一個(gè)PHP功能模塊的運(yùn)行細(xì)節(jié),搭建一個(gè)PHP調(diào)試環(huán)境,然后單步調(diào)試程序,以掌握程序的整個(gè)運(yùn)行過程顯的尤為重要2014-01-01php中sprintf與printf函數(shù)用法區(qū)別解析
這篇文章主要介紹了php中sprintf與printf函數(shù)用法區(qū)別解析,需要的朋友可以參考下2014-02-02jquery獲取多個(gè)checkbox的值異步提交給php的方法
這篇文章主要介紹了jquery獲取多個(gè)checkbox的值異步提交給php的方法,涉及jQuery操作頁(yè)面元素進(jìn)行異步傳輸?shù)南嚓P(guān)技巧,需要的朋友可以參考下2015-06-06PHP實(shí)現(xiàn)二叉樹深度優(yōu)先遍歷(前序、中序、后序)和廣度優(yōu)先遍歷(層次)實(shí)例詳解
這篇文章主要介紹了PHP實(shí)現(xiàn)二叉樹深度優(yōu)先遍歷(前序、中序、后序)和廣度優(yōu)先遍歷(層次),結(jié)合實(shí)例形式詳細(xì)分析了php針對(duì)二叉樹的深度優(yōu)先遍歷與廣度優(yōu)先遍歷相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-04-04實(shí)現(xiàn)PHP多線程異步請(qǐng)求的3種方法
實(shí)現(xiàn)PHP多線程異步請(qǐng)求的方法有很多,在本文整理了3種不多的常用方法,大家可以參考下2014-01-01php使用QueryList輕松采集js動(dòng)態(tài)渲染頁(yè)面方法
這篇文章給大家分享了php使用QueryList輕松采集js動(dòng)態(tài)渲染頁(yè)面的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友學(xué)習(xí)下。2018-09-09