PHP 7.1 新特性____PHP

來源:互聯網
上載者:User

從PHP 7.0.x 移植到 PHP 7.1.x

新特性

http://php.net/manual/en/migration71.new-features.php


新特性 ¶ 可為空白(Nullable)類型 ¶

類型現在允許為空白,當啟用這個特性時,傳入的參數或者函數返回的結果要麼是給定的類型,要麼是 null 。可以通過在類型前面加上一個問號來使之成為可為空白的。 <?php
function test(?string $name)
{
    var_dump($name);
}

test('tpunt');
test(null);
test();

以上常式會輸出:

string(5) "tpunt"NULLUncaught Error: Too few arguments to function test(), 0 passed in...
Void 函數 ¶

在PHP 7 中引入的其他傳回值類型的基礎上,一個新的傳回值類型void被引入。 傳回值聲明為 void 類型的方法要麼乾脆省去 return 語句,要麼使用一個空的 return 語句。 對於 void 函數來說,null 不是一個合法的傳回值。 <?php
function swap(&$left, &$right) : void
{
    if ($left === $right) {
        return;
    }

    $tmp = $left;
    $left = $right;
    $right = $tmp;
}

$a = 1;
$b = 2;
var_dump(swap($a, $b), $a, $b);

以上常式會輸出:

nullint(2)int(1)

試圖去擷取一個 void 方法的傳回值會得到 null ,並且不會產生任何警告。這麼做的原因是不想影響更高層次的方法。 Symmetric array destructuring ¶

短數組文法([])現在可以用於將數組的值賦給一些變數(包括在foreach中)。 這種方式使從數組中提取值變得更為容易。 <?php
$data = [
    ['id' => 1, 'name' => 'Tom'],
    ['id' => 2, 'name' => 'Fred'],
];

while (['id' => $id, 'name' => $name] = $data) {
    // logic here with $id and $name
} 類常量可見度 ¶

現在起支援設定類常量的可見度。 <?php
class ConstDemo
{
    const PUBLIC_CONST_A = 1;
    public const PUBLIC_CONST_B = 2;
    protected const PROTECTED_CONST = 3;
    private const PRIVATE_CONST = 4;
} iterable 偽類 ¶

現在引入了一個新的被稱為iterable的偽類 (與callable類似)。 這可以被用在參數或者傳回值類型中,它代表接受數組或者實現了Traversable介面的對象。 至於子類,當用作參數時,子類可以收緊父類的iterable類型到array 或一個實現了Traversable的對象。對於傳回值,子類可以拓寬父類的 array或對象傳回值類型到iterable。 <?php
function iterator(iterable $iter)
{
    foreach ($iter as $val) {
        //
    }
} 多異常捕獲處理 ¶

一個catch語句塊現在可以通過管道字元(|)來實現多個異常的捕獲。 這對於需要同時處理來自不同類的不同異常時很有用。 <?php
try {
    // some code
} catch (FirstException | SecondException $e) {
    // handle first and second exceptions
} list()現在支援鍵名 ¶

現在list()支援在它內部去指定鍵名。這意味著它可以將任意類型的數組 都賦值給一些變數(與短數組文法類似) <?php
$data = [
    ['id' => 1, 'name' => 'Tom'],
    ['id' => 2, 'name' => 'Fred'],
];

while (list('id' => $id, 'name' => $name) = $data) {
    // logic here with $id and $name
} 支援為負的字串位移量 ¶

現在所有支援位移量的字串操作函數 都支援接受負數作為位移量,包括通過[]或{}操作字串下標。在這種情況下,一個負數的位移量會被理解為一個從字串結尾開始的位移量。 <?php
var_dump("abcdef"[-2]);
var_dump(strpos("aabbcc", "b", -3));

以上常式會輸出:

string (1) "e"int(3)

Negative string and array offsets are now also supported in the simple variable parsing syntax inside of strings. <?php
$string = 'bar';
echo "The last character of '$string' is '$string[-1]'.\n";
?>

以上常式會輸出:

The last character of 'bar' is 'r'.
ext/openssl 支援 AEAD ¶

通過給openssl_encrypt()和openssl_decrypt() 添加額外參數,現在支援了AEAD (模式 GCM and CCM)。 通過 Closure::fromCallable() 將callables轉為閉包 ¶

Closure新增了一個靜態方法,用於將callable快速地 轉為一個Closure 對象。 <?php
class Test
{
    public function exposeFunction()
    {
        return Closure::fromCallable([$this, 'privateFunction']);
    }

    private function privateFunction($param)
    {
        var_dump($param);
    }
}

$privFunc = (new Test)->exposeFunction();
$privFunc('some value');

以上常式會輸出:

string(10) "some value"
非同步訊號處理 ¶

一個新的名為 pcntl_async_signals() 的方法現在被引入, 用於啟用無需 ticks (這會帶來很多額外的開銷)的非同步訊號處理。 <?php
pcntl_async_signals(true); // turn on async signals

pcntl_signal(SIGHUP,  function($sig) {
    echo "SIGHUP\n";
});

posix_kill(posix_getpid(), SIGHUP);

以上常式會輸出:

SIGHUP
HTTP/2 server push support in ext/curl ¶

對伺服器推送的支援現在已經被加入到 CURL 擴充中( 需要版本 7.46 或更高)。這個可以通過 curl_multi_setopt() 函數與新的常量 CURLMOPT_PUSHFUNCTION 來進行調節。常量 CURL_PUST_OK 和 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.