《PHP應用:PHP 7.1新特性的匯總介紹》要點:
本文介紹了PHP應用:PHP 7.1新特性的匯總介紹,希望對您有用。如果有疑問,可以聯系我們。
PHP應用一、可空類型
PHP應用可空類型主要用于參數類型聲明和函數返回值聲明.
PHP應用主要的兩種形式如下:
PHP應用
<?php
function answer(): ?int {
return null; //ok
}
function answer(): ?int {
return 42; // ok
}
function say(?string $msg) {
if ($msg) {
echo $msg;
}
}
PHP應用從例子很容易理解,所指的就是通過 ? 的形式表明函數參數或者返回值的類型要么為指定類型,要么為 null.
PHP應用此方法也可用于接口函數的定義:
PHP應用
<?php
interface Fooable {
function foo(?Fooable $f);
}
PHP應用但有一個需要注意的地方:如果函數本身定義了參數類型并且沒有默認值,即使是可空的,也不能省略,否則會觸發錯誤.如下:
PHP應用
<?php
function foo_nullable(?Bar $bar) {}
foo_nullable(new Bar); // 可行
foo_nullable(null); // 可行
foo_nullable(); // 不可行
PHP應用但是如果以上函數的參數定義為 ?Bar $bar = null
的形式,則第三種寫法也是可行的.因為 = null
實際上相當于 ? 的超集,對于可空類型的參數,可以設定 null 為默認值.
PHP應用二、list 的方括號簡寫
PHP應用我們知道在 PHP5.4 之前只能通過 array()
來定義數組,5.4之后添加了 [] 的簡化寫法(省略了5個字符還是很實在的).
PHP應用
<?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];
PHP應用引申到另外一個問題上,如果我們要把數組的值賦值給不同的變量,可以通過 list
來實現:
PHP應用
<?php
list($a, $b, $c) = $array;
PHP應用是否也可以通過 [] 的簡寫來實現呢?
PHP應用
<?php
[$a, $b, $c] = $array;
PHP應用以及下一個特性中會提到的 list
指定 key
:
PHP應用
<?php
["a" => $a, "b" => $b, "c" => $c] = $array;
PHP應用PHP7.1 實現了這個特性.但是要注意的是:出現在左值中的 [] 并不是數組的簡寫,是 list()
的簡寫.
PHP應用但是并不僅僅如此,新的 list()
的實現并不僅僅可以出現在左值中,也能在 foreach
循環中使用:
PHP應用
<?php
foreach ($points as ["x" => $x, "y" => $y]) {
var_dump($x, $y);
}
PHP應用不過因為實現的問題,list()
和 [] 不能相互嵌套使用:
PHP應用
<?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]];
PHP應用三、允許在 list 中指定 key
PHP應用上文提到過,新的 list()
的實現中可以指定key:
PHP應用
<?php
$array = ["a" => 1, "b" => 2, "c" => 3];
["a" => $a, "b" => $b, "c" => $c] = $array;
PHP應用這也就相當于:
PHP應用
<?php
$a = $array['a'];
$b = $array['b'];
$c = $array['c'];
PHP應用和以往的區別在于以往的 list()
的實現相當于 key 只能是 0, 1, 2, 3 的數字形式并且不能調整順序.執行以下語句:
PHP應用
<?php
list($a, $b) = [1 => '1', 2 => '2'];
PHP應用會得到 PHP error: Undefined offset: 0...
的錯誤.
PHP應用而新的實現則可以通過以下方式來調整賦值:
PHP應用
<?php
list(1 => $a, 2 => $b) = [1 => '1', 2 => '2'];
PHP應用不同于數組的是,list
并不支持混合形式的 key
,以下寫法會觸發解析錯誤:
PHP應用
<?php
// Parse error: syntax error, ...
list($unkeyed, "key" => $keyed) = $array;
PHP應用更復雜的情況,list
也支持復合形式的解析:
PHP應用
<?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;
PHP應用以及循環中使用:
PHP應用
<?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;
}
PHP應用四、void 返回類型
PHP應用PHP7.0 添加了指定函數返回類型的特性,但是返回類型卻不能指定為 void
,7.1 的這個特性算是一個補充:
PHP應用
<?php
function should_return_nothing(): void {
return 1; // Fatal error: A void function must not return a value
}
PHP應用以下兩種情況都可以通過驗證:
PHP應用
<?php
function lacks_return(): void {
// valid
}
function returns_nothing(): void {
return; // valid
}
PHP應用定義返回類型為 void
的函數不能有返回值,即使返回 null
也不行:
PHP應用
<?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
}
PHP應用此外 void
也只適用于返回類型,并不能用于參數類型聲明,或者會觸發錯誤:
PHP應用
<?php
function foobar(void $foo) { // Fatal error: void cannot be used as a parameter type
}
PHP應用類函數中對于返回類型的聲明也不能被子類覆蓋,否則會觸發錯誤:
PHP應用
<?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
}
}
PHP應用五、類常量屬性設定
PHP應用這個特性說起來比較簡單,就是現在類中的常量支持使用 public
、private
和 protected
修飾了:
PHP應用
<?php
class Token {
// 常量默認為 public
const PUBLIC_CONST = 0;
// 可以自定義常量的可見范圍
private const PRIVATE_CONST = 0;
protected const PROTECTED_CONST = 0;
public const PUBLIC_CONST_TWO = 0;
// 多個常量同時聲明只能有一個屬性
private const FOO = 1, BAR = 2;
}
PHP應用此外,接口(interface
)中的常量只能是 public
屬性:
PHP應用
<?php
interface ICache {
public const PUBLIC = 0;
const IMPLICIT_PUBLIC = 1;
}
PHP應用為了應對變化,反射類的實現也相應的豐富了一下,增加了 getReflectionConstant
和 getReflectionConstants
兩個方法用于獲取常量的額外屬性:
PHP應用
<?php
class testClass {
const TEST_CONST = 'test';
}
$obj = new ReflectionClass( "testClass" );
$const = $obj->getReflectionConstant( "TEST_CONST" );
$consts = $obj->getReflectionConstants();
PHP應用六、多條件 catch
PHP應用在以往的 try ... catch
語句中,每個 catch
只能設定一個條件判斷:
PHP應用
<?php
try {
// Some code...
} catch (ExceptionType1 $e) {
// 處理 ExceptionType1
} catch (ExceptionType2 $e) {
// 處理 ExceptionType2
} catch (\Exception $e) {
// ...
}
PHP應用新的實現中可以在一個 catch
中設置多個條件,相當于或的形式判斷:
PHP應用
<?php
try {
// Some code...
} catch (ExceptionType1 | ExceptionType2 $e) {
// 對于 ExceptionType1 和 ExceptionType2 的處理
} catch (\Exception $e) {
// ...
}
PHP應用對于異常的處理簡化了一些.
PHP應用總結
PHP應用以上就是這篇文章的全部內容了,希望本文的內容對大家學習或者使用PHP7.1能有一定的幫助,如果有疑問大家可以留言交流.
PHP應用附:源 RFC 地址
PHP應用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
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/2290.html