1. null合并運(yùn)算符(??)
語(yǔ)法: 如果變量存在且值不為NULL,它就會(huì)返回自身的值,否則返回它的第二個(gè)操作數(shù).
//php7以前 if判斷 if(empty($_GET['param'])) { $param = 1; }else{ $param = $_GET['param']; } //php7以前 三元運(yùn)算符 $param = empty($_GET['param']) ? 1 : $_GET['param']; //PHP7 null合并運(yùn)算符 $param = $_GET['param'] ?? 1;//1
2. define() 定義常量數(shù)組
//php7以前 define("CONTENT", "hello world"); echo CONTENT;//hello world //PHP7 define('ANIMALS', [ 'dog', 'cat', 'bird' ]); echo ANIMALS[2];//bird //PHP7 類外也可使用const來(lái)定義常量 const CONSTANT = 'Hello World'; echo CONSTANT;//Hello World
3. 組合比較符(=>)
組合比較符用于比較兩個(gè)表達(dá)式.當(dāng)$a小于、等于或大于$b時(shí)它分別返回-1、0或1. 比較的原則是沿用PHP的常規(guī)比較規(guī)則進(jìn)行的.
/整數(shù) echo 1 => 1; // 0 echo 1 => 2; // -1 echo 2 => 1; // 1 //浮點(diǎn)數(shù) echo 1.5 => 1.5; // 0 echo 1.5 => 2.5; // -1 echo 2.5 => 1.5; // 1 //字符串 echo "a" => "a"; // 0 echo "a" => "b"; // -1 echo "b" => "a"; // 1
4. 變量類型聲明
兩種模式: 強(qiáng)制(默認(rèn))和嚴(yán)格模式. 可以使用下列類型參數(shù): string,int,float,bool
//... 操作符: 表示這是一個(gè)可變參數(shù). php5.6及以上的版本可使用: 函數(shù)定義的時(shí)候變量前使用. function intSum(int ...$ints){ return array_sum($ints); } var_dump(intSum(2,'3.5'));//5 //嚴(yán)格模式 //模式聲明:declare(strict_types=1); 默認(rèn)情況值為0,值為1代表為嚴(yán)格校驗(yàn)的模式 declare(strict_types=1); function add(int $a,int $b){ return $a+$b; } var_dump(add(2,'3.5')); //Fatal error: Uncaught TypeError: Argument 2 passed to add() must be of the type integer
5. 返回值類型聲明
增加返回類型聲明的支持.類似于參數(shù)類型聲明.(用法在函數(shù)定義的后面加 :類型名)
//有效的返回類型 declare(strict_types = 1); function getInt(int $value): int { return $value; } print(getInt(6));//6
//無(wú)效返回類型 declare(strict_types = 1); function getNoInt(int $value): int { return $value+'2.5'; } print(getNoInt(6));//Fatal error: Uncaught TypeError: Return value of getNoInt() must be of the type integer
6. 匿名類
允許new class {} 創(chuàng)建一個(gè)匿名的對(duì)象.
?php //php7以前 接口實(shí)現(xiàn) interface User{ public function getDiscount(); } class VipUser implements User{ //折扣系數(shù) private $discount = 0.6; public function getDiscount() { return $this->discount; } } class Goods{ private $price = 200; private $objectVipUser; //User接口VipUser類實(shí)現(xiàn) public function getUserData($User){ $this->objectVipUser = $User; $discount = $this->objectVipUser->getDiscount(); echo "商品價(jià)格:".$this->price*$discount; } } $display = new Goods(); //常規(guī)實(shí)例化接口實(shí)現(xiàn)對(duì)象 $display ->getUserData(new VipUser);//商品價(jià)格:120
?php //php7 創(chuàng)建一個(gè)匿名的對(duì)象 interface User{ public function getDiscount(); } class Goods{ private $price = 200; private $objectVipUser; public function getUserData($User){ $this->objectVipUser = $User; $discount = $this->objectVipUser->getDiscount(); echo "商品價(jià)格:".$this->price*$discount; } } $display = new Goods(); //new匿名對(duì)象實(shí)現(xiàn)user接口 $display ->getUserData(new class implements User{ private $discount = 0.6; public function getDiscount() { return $this->discount; } });//商品價(jià)格:120
7. Closure::call()
Closure::call() 方法被添加為一個(gè)簡(jiǎn)短的方式來(lái)臨時(shí)綁定一個(gè)對(duì)象作用域到一個(gè)閉包并調(diào)用它. 與PHP5的bindTo相比.它的性能要快得多.
?php //php7以前 class A { private $attribute = 'hello world'; } $getClosure = function(){ return $this->attribute; }; $getAttribute = $getClosure->bindTo(new A, 'A');//中間層閉包 echo $getAttribute();//hello world
?php //PHP7 class A { private $attribute = 'hello world'; } $getClosure = function(){ return $this->attribute; }; echo $getClosure->call(new A);//hello world
8. unserialize()
unserialize()函數(shù):過(guò)濾的特性,可以防止非法數(shù)據(jù)進(jìn)行代碼注入,提供了更安全的反序列化數(shù)據(jù)
?php class A{ public $name = 'admin_a'; } class B{ public $name = 'admin_b'; } $objA = new A(); $objB = new B(); $serializedObjA = serialize($objA); $serializedObjB = serialize($objB); //默認(rèn)行為是接收所有類; 第二個(gè)參數(shù)可以忽略 $dataA = unserialize($serializedObjA , ["allowed_classes" => true]); var_dump($dataA);//object(A)#3 (1) { ["name"]=> string(7) "admin_a" } //如果allowed_classes設(shè)置為false,unserialize會(huì)將所有對(duì)象轉(zhuǎn)換為_(kāi)_PHP_Incomplete_Class對(duì)象 $dataA = unserialize($serializedObjA , ["allowed_classes" => false]); var_dump($dataA);//object(__PHP_Incomplete_Class)#4 (2) { ["__PHP_Incomplete_Class_Name"]=> string(1) "A" ["name"]=> string(7) "admin_a" } //轉(zhuǎn)換所有對(duì)象到 __PHP_Incomplete_Class對(duì)象,除了對(duì)象"B" $dataB = unserialize($serializedObjB , ["allowed_classes" => ["B"]]); var_dump($dataB);//object(B)#3 (1) { ["name"]=> string(7) "admin_b" }
9. IntlChar
IntlChar:提供了一些可用于訪問(wèn)Unicode字符信息的實(shí)用方法的訪問(wèn). 注意:必須安裝Intl擴(kuò)展才能使用!
var_dump(IntlChar::CODEPOINT_MAX);//int(1114111) echo 'br>'; var_dump(IntlChar::charName('+'));//string(9) "PLUS SIGN" echo 'br>'; var_dump(IntlChar::ispunct('?'));//bool(true)
10. CSPRNG
CSPRNG 函數(shù)提供一種簡(jiǎn)單的機(jī)制來(lái)生成密碼的隨機(jī)數(shù).
random_bytes() -加密生存被保護(hù)的偽隨機(jī)字符串.
random_int() -加密生存被保護(hù)的偽隨機(jī)整數(shù).
$bytes = random_bytes(8); echo(bin2hex($bytes));//隨機(jī)2073a110a2e3c497 echo 'br>'; echo(random_int(1, 999));//隨機(jī)786 echo 'br>'; print(random_int(-999, -1));//隨機(jī)-357
11. use 語(yǔ)句
可以使用單個(gè)use語(yǔ)句從相同的命名空間導(dǎo)入類,函數(shù)和常量,而不是使用多個(gè)use語(yǔ)句.
//PHP7之前 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; // PHP7之后 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};
12. intp
新增加intp()函數(shù),接收兩個(gè)參數(shù),返回值為第一個(gè)參數(shù)除于第二個(gè)參數(shù)的值并取整.
echo intp(8,4);//2 echo intp(10,4);//2 echo intp(5,10);//0
13. PHP7 錯(cuò)誤處理
PHP7 改變了大多數(shù)錯(cuò)誤的報(bào)告方式.不同于PHP5的傳統(tǒng)錯(cuò)誤報(bào)告機(jī)制,現(xiàn)在大多數(shù)錯(cuò)誤被作為Error異常拋出.
這種Error異??梢韵衿胀ó惓R粯颖籺ry / catch塊所捕獲. 如果沒(méi)有匹配的try / catch塊,則調(diào)用異常處理函數(shù)(由 set_exception_handler() 注冊(cè))進(jìn)行處理.
如果尚未注冊(cè)異常處理函數(shù),則按照傳統(tǒng)方式處理:被報(bào)告為一個(gè)致命錯(cuò)誤(Fatal Error).
Error類并不是從Exception類擴(kuò)展出來(lái)的,所以用catch (Exception $e) { ... } 這樣的代碼是捕獲不到Error的.你可以用 catch (Error $e) { ... } 這樣的代碼,
或者通過(guò)注冊(cè)異常處理函數(shù)( set_exception_handler())來(lái)捕獲Error.
?php //php7以前 自定義異常處理 class getException extends Exception{ public function errorMsg(){ return '錯(cuò)誤的信息'.$this->getMessage().'br>錯(cuò)誤的代碼'.$this->getCode(); } } try { $num =10; if($num > 1) { throw new getException($num,404); } } catch (getException $e) { echo $e->errorMsg(); }
?php //php7 異常處理 try { test(); }catch(Error $e) { echo $e->getMessage();//Call to undefined function test() }
標(biāo)簽:韶關(guān) 九江 昭通 涼山 甘肅 遼陽(yáng) 梅河口 十堰
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php7新特性的理解和比較總結(jié)》,本文關(guān)鍵詞 php7,新特,性的,理解,和,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。