国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

PHP7新機能マニュアル / PHP7新增功能

PHP7新增功能

PHP7新增功能


一、核心

  • 增加了group use語法聲明。RFC: https://wiki.php.net/rfc/group_use_declarations
  • 增加了null合并運算符??。RFC: https://wiki.php.net/rfc/isset_ternary
  • 64位PHP7字符串長度可以超過2^31次方字節(jié)。
  • 增加了Closure::call()方法。
  • 雙引號字符串和heredocs里面支持使用\u{xxxxx}來聲明unicode字符。
  • define()可以把一個數(shù)組定義為常量。
  • 增加了合并比較運算符<=>。RFC: https://wiki.php.net/rfc/combined-comparison-operator
  • 增加了yield from 操作符。https://wiki.php.net/rfc/generator-delegation
  • 關(guān)鍵詞在特定的場景中也可以使用了。RFC: https://wiki.php.net/rfc/context_sensitive_lexer
  • 增加了標(biāo)量類型聲明功能。RFC: https://wiki.php.net/rfc/scalar_type_hints_v5
  • 增加接口為用戶層提供安全方便的隨機數(shù)生成器。RFC: https://wiki.php.net/rfc/easy_userland_csprng


①PHP標(biāo)量類型與返回值類型聲明

標(biāo)量類型聲明

默認情況下,所有的PHP文件都處于弱類型校驗?zāi)J健?/p>

PHP 7 增加了標(biāo)量類型聲明的特性,標(biāo)量類型聲明有兩種模式:

  • 強制模式 (默認)嚴格模式

  • 嚴格模式

標(biāo)量類型聲明語法格式:

declare(strict_types=1);

代碼中通過指定 strict_types的值(1或者0),1表示嚴格類型校驗?zāi)J?,作用于函?shù)調(diào)用和返回語句;0表示弱類型校驗?zāi)J健?/p>

可以使用的類型參數(shù)有:

  • int

  • float

  • bool

  • string

  • interfaces

  • array

  • callable

強制模式實例:

<?php
// 強制模式
function sum(int ...$ints)
{
   return array_sum($ints);
}
print(sum(2, '3', 4.1));
?>

以上程序執(zhí)行輸出結(jié)果為:

9

實例匯總將參數(shù) 4.1 轉(zhuǎn)換為整數(shù) 4 后再相加。

嚴格模式實例:

<?php
// 嚴格模式
declare(strict_types=1);

function sum(int ...$ints)
{
   return array_sum($ints);
}

print(sum(2, '3', 4.1));
?>

以上程序由于采用了嚴格模式,所以如果參數(shù)中出現(xiàn)不適整數(shù)的類型會報錯,執(zhí)行輸出結(jié)果為:

PHP Fatal error:  Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given, called in……

返回類型聲明

PHP 7 增加了對返回類型聲明的支持,返回類型聲明指明了函數(shù)返回值的類型。

可以聲明的返回類型有:

  • int

  • float

  • bool

  • string

  • interfaces

  • array

  • callable

返回類型聲明實例:

實例中,要求返回結(jié)果為整數(shù):

<?php
declare(strict_types=1);
function returnIntValue(int $value): int
{
   return $value;
}
print(returnIntValue(5));
?>

以上程序執(zhí)行輸出結(jié)果為:

5

返回類型聲明錯誤實例:

<?php
declare(strict_types=1);

function returnIntValue(int $value): int
{
   return $value + 1.0;
}

print(returnIntValue(5));
?>

以上程序由于采用了嚴格模式,返回值必須是 int,但是計算結(jié)果是float,所以會報錯,執(zhí)行輸出結(jié)果為:

Fatal error: Uncaught TypeError: Return value of returnIntValue() must be of the type integer, float returned...

②PHP NULL 合并運算符

PHP 7 新增加的 NULL 合并運算符(??)是用于執(zhí)行isset()檢測的三元運算的快捷方式。

NULL 合并運算符會判斷變量是否存在且值不為NULL,如果是,它就會返回自身的值,否則返回它的第二個操作數(shù)。

以前我們這樣寫三元運算符:

$site = isset($_GET['site']) ? $_GET['site'] : 'PHP中文網(wǎng)';

現(xiàn)在我們可以直接這樣寫:

$site = $_GET['site'] ?? 'PHP中文網(wǎng)';

實例

<?php
// 獲取 $_GET['site'] 的值,如果不存在返回 'PHP中文網(wǎng)'
$site = $_GET['site'] ?? 'PHP中文網(wǎng)';

print($site);
print(PHP_EOL); // PHP_EOL 為換行符

// 以上代碼等價于
$site = isset($_GET['site']) ? $_GET['site'] : 'PHP中文網(wǎng)';

print($site);
print(PHP_EOL);
// ?? 鏈
$site = $_GET['site'] ?? $_POST['site'] ?? 'PHP中文網(wǎng)';

print($site);
?>

以上程序執(zhí)行輸出結(jié)果為:

PHP中文網(wǎng)
PHP中文網(wǎng)
PHP中文網(wǎng)

③PHP 太空船運算符(組合比較符)

PHP 7 新增加的太空船運算符(組合比較符)用于比較兩個表達式 $a 和 $b,如果 $a 小于、等于或大于 $b時,它分別返回-1、0或1。

實例

<?php
// 整型比較
print( 1 <=> 1);print(PHP_EOL);
print( 1 <=> 2);print(PHP_EOL);
print( 2 <=> 1);print(PHP_EOL);
print(PHP_EOL); // PHP_EOL 為換行符

// 浮點型比較
print( 1.5 <=> 1.5);print(PHP_EOL);
print( 1.5 <=> 2.5);print(PHP_EOL);
print( 2.5 <=> 1.5);print(PHP_EOL);
print(PHP_EOL);

// 字符串比較
print( "a" <=> "a");print(PHP_EOL);
print( "a" <=> "b");print(PHP_EOL);
print( "b" <=> "a");print(PHP_EOL);
?>

以上程序執(zhí)行輸出結(jié)果為:

0
-1
1

0
-1
1

0
-1
1

④PHP 常量數(shù)組

在 PHP 5.6 中僅能通過 const 定義常量數(shù)組,PHP 7 可以通過 define() 來定義。

實例

<?php// 使用 define 函數(shù)來定義數(shù)組define('sites', [
   'Google',
   'PHP',
   'Taobao']);print(sites[1]);?>

以上程序執(zhí)行輸出結(jié)果為:

PHP

⑤PHP Closure::call()

PHP 7 的 Closure::call() 有著更好的性能,將一個閉包函數(shù)動態(tài)綁定到一個新的對象實例并調(diào)用執(zhí)行該函數(shù)。

實例

<?php
class A {
    private $x = 1;
}

// PHP 7 之前版本定義閉包函數(shù)代碼
$getXCB = function() {
    return $this->x;
};

// 閉包函數(shù)綁定到類 A 上
$getX = $getXCB->bindTo(new A, 'A'); 

echo $getX();
print(PHP_EOL);

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

以上程序執(zhí)行輸出結(jié)果為:

1
1

⑥PHP CSPRNG

CSPRNG(Cryptographically Secure Pseudo-Random Number Generator,偽隨機數(shù)產(chǎn)生器)。

PHP 7 通過引入幾個 CSPRNG 函數(shù)提供一種簡單的機制來生成密碼學(xué)上強壯的隨機數(shù)。

  • random_bytes() - 加密生存被保護的偽隨機字符串。

  • random_int() - 加密生存被保護的偽隨機整數(shù)

random_bytes()

語法格式

string random_bytes ( int $length )

參數(shù)

  • length - 隨機字符串返回的字節(jié)數(shù)。

返回值

  • 返回一個字符串,接受一個int型入?yún)⒋矸祷亟Y(jié)果的字節(jié)數(shù)。

實例

<?php
$bytes = random_bytes(5);
print(bin2hex($bytes));
?>

以上程序執(zhí)行輸出結(jié)果為:

6f36d48a29

random_int()

語法格式

int random_int ( int $min , int $max )

參數(shù)

  • min - 返回的最小值,必須是大于或等于 PHP_INT_MIN 。

  • max - 返回的最大值,必須是小于或等于 PHP_INT_MAX 。

返回值

  • 返回一個指定范圍內(nèi)的int型數(shù)字。

實例

<?php
print(random_int(100, 999));
print(PHP_EOL);
print(random_int(-1000, 0));
?>

以上程序執(zhí)行輸出結(jié)果為:

723
-64

⑦PHP 匿名類

PHP 7 支持通過 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;
// 使用 new class 創(chuàng)建匿名類
$app->setLogger(new class implements Logger {
   public function log(string $msg) {
      print($msg);
   }
});
$app->getLogger()->log("我的第一條日志");
?>

以上程序執(zhí)行輸出結(jié)果為:

我的第一條日志

⑧PHP 7 use 語句

PHP 7 可以使用一個 use 從同一個 namespace 中導(dǎo)入類、函數(shù)和常量:

// PHP 7 之前版本需要使用多次 use
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;

use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;

use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC;

// PHP 7+ 之后版本可以使用一個 use 導(dǎo)入同一個 namespace 的類
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};
?>

二、Opcache模塊

  • 增加了基于文件的二級opcode 緩存機制。可以在php.ini文件中設(shè)置opcache.file_cache=<DIR>。當(dāng)服務(wù)重啟或者SHM重置的時候,使用二級文件緩存機制可以提高性能。
  • 也可以設(shè)置opcache.file_cache_only=1來限定只使用文件緩存。
  • 可以設(shè)置opcache.file_cache_consistency_checks=0參數(shù)來加快加載速度。
  • 可以設(shè)置opcache.huge_code_pages=0/1來決定是否將PHP code pages放到huage pages里面。http://www.laruence.com/2015/10/02/3069.html
  • windows版本增加了opcache.file_cache_fallback=1 配置項。

三、OpenSSL模塊

增加了"alpn_protocols”選項。

四、反射

  • 增加了ReflectionGenerator類,用于yield from Traces, current file/line等等。
  • 增加了ReflectionType類,更好的支持新的返回值和標(biāo)量聲明功能。

五、流

windows版本增加了塊讀取的選項??梢酝ㄟ^傳遞array("pipe" => array("blocking" => true))參數(shù)來激活。