PHP 7.0 新特性____PHP

來源:互聯網
上載者:User

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

新特性

http://php.net/manual/zh/migration70.new-features.php


新特性 ¶ 標量型別宣告 ¶

標量型別宣告 有兩種模式: 強制 (預設) 和 strict 模式。 現在可以使用下列型別參數(無論用強制模式還是strict 模式): 字串(string), 整數 (int), 浮點數 (float), 以及布爾值 (bool)。它們擴充了PHP5中引入的其他類型:類名,介面,數組和 回調類型。 <?php
// Coercive mode
function sumOfInts(int ...$ints)
{
    return array_sum($ints);
}

var_dump(sumOfInts(2, '3', 4.1));

以上常式會輸出:

int(9)

要使用strict 模式,一個 declare 聲明指令必須放在檔案的頂部。這意味著嚴格聲明標量是基於檔案可配的。 這個指令不僅影響參數的型別宣告,也影響到函數的傳回值聲明(參見 傳回值型別宣告, 內建的PHP函數以及擴充中載入的PHP函數)

完整的標量型別宣告文檔和樣本參見型別宣告章節。 傳回值型別宣告 ¶

PHP 7 增加了對傳回型別聲明的支援。 類似於參數型別宣告,傳回型別聲明指明了函數傳回值的類型。可用的類型與參數聲明中可用的類型相同。 <?php

function arraysSum(array ...$arrays): array
{
    return array_map(function(array $array): int {
        return array_sum($array);
    }, $arrays);
}

print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));

以上常式會輸出:

Array(    [0] => 6    [1] => 15    [2] => 24)

完整的標量型別宣告文檔和樣本可參見 傳回值型別宣告. null合并運算子 ¶

由於日常使用中存在大量同時使用三元運算式和 isset()的情況, 我們添加了null合并運算子 (??) 這個文法糖。如果變數存在且值不為NULL, 它就會返回自身的值,否則返回它的第二個運算元。 <?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';

// Coalesces can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
?> 太空船操作符(組合比較符) ¶

太空船操作符用於比較兩個運算式。當$a小於、等於或大於$b時它分別返回-1、0或1。 比較的原則是沿用 PHP 的常規比較規則進行的。 <?php
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
 
// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
?> 通過 define() 定義常量數組 ¶

Array 類型的常量現在可以通過 definedefine() 來定義。在 PHP5.6 中僅能通過 const定義。 <?php
define('ANIMALS', [
    'dog',
    'cat',
    'bird'
]);

echo ANIMALS[1]; // outputs "cat"
?> 匿名類 ¶

現在支援通過new class 來執行個體化一個匿名類,這可以用來替代一些“用後即焚”的完整類定義。 <?php
interface Logger {
    public function log(string $msg);
}

class Application {
    private $logger;

    public function getLogger(): Logger {
         return $this->logger;
    }

    public function setLogger(Logger $logger) {
         $this->logger = $logger;
    }
}

$app = new Application;
$app->setLogger(new class implements Logger {
    public function log(string $msg) {
        echo $msg;
    }
});

var_dump($app->getLogger());
?>

以上常式會輸出:

object(class@anonymous)#2 (0) {}

詳細文檔可以參考 匿名類. Unicode codepoint 轉譯文法 ¶

這接受一個以16進位形式的 Unicode codepoint,並列印出一個雙引號或heredoc包圍的 UTF-8 編碼格式的字串。 可以接受任何有效 codepoint,並且開頭的 0 是可以省略的。 echo "\u{aa}";
echo "\u{0000aa}";
echo "\u{9999}";

以上常式會輸出:

ªª (same as before but with optional leading 0's)香
Closure::call() ¶

Closure::call() 現在有著更好的效能,簡短幹練的暫時綁定一個方法到對象上閉包並調用它。 <?php
class A {private $x = 1;}

// Pre PHP 7 code
$getXCB = function() {return $this->x;};
$getX = $getXCB->bindTo(new A, 'A'); // intermediate closure
echo $getX();

// PHP 7+ code
$getX = function() {return $this->x;};
echo $getX->call(new A);

以上常式會輸出:

11
為unserialize()提供過濾 ¶

這個特性旨在提供更安全的方式解包不可靠的資料。它通過白名單的方式來防止潛在的代碼注入。 <?php

// converts all objects into __PHP_Incomplete_Class object
$data = unserialize($foo, ["allowed_classes" => false]);

// converts all objects into __PHP_Incomplete_Class object except those of MyClass and MyClass2
$data = unserialize($foo, ["allowed_classes" <

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.