您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)php7新特性的示例分析的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
1、 null合并運(yùn)算符(??)
??語法: 如果變量存在且值不為NULL,它就會返回自身的值,否則返回它的第二個(gè)操作數(shù).
1 //php7以前 if判斷 2 if(empty($_GET['param'])) { 3 $param = 1; 4 }else{ 5 $param = $_GET['param']; 6 } 7 8 //php7以前 三元運(yùn)算符 9 $param = empty($_GET['param']) ? 1 : $_GET['param'];10 11 //PHP7 null合并運(yùn)算符12 $param = $_GET['param'] ?? 1;//1
2. define() 定義常量數(shù)組
1 //php7以前 2 define("CONTENT", "hello world"); 3 echo CONTENT;//hello world 4 5 //PHP7 6 define('ANIMALS', [ 7 'dog', 8 'cat', 9 'bird'10 ]);11 echo ANIMALS[2];//bird12 13 //PHP7 類外也可使用const來定義常量14 const CONSTANT = 'Hello World'; 15 echo CONSTANT;//Hello World
3. 組合比較符(<=>)
組合比較符用于比較兩個(gè)表達(dá)式.當(dāng)$a小于、等于或大于$b時(shí)它分別返回-1、0或1. 比較的原則是沿用PHP的常規(guī)比較規(guī)則進(jìn)行的.
1 //整數(shù) 2 echo 1 <=> 1; // 0 3 echo 1 <=> 2; // -1 4 echo 2 <=> 1; // 1 5 6 //浮點(diǎn)數(shù) 7 echo 1.5 <=> 1.5; // 0 8 echo 1.5 <=> 2.5; // -1 9 echo 2.5 <=> 1.5; // 110 11 //字符串12 echo "a" <=> "a"; // 013 echo "a" <=> "b"; // -114 echo "b" <=> "a"; // 1
4. 變量類型聲明
兩種模式: 強(qiáng)制(默認(rèn))和嚴(yán)格模式. 可以使用下列類型參數(shù): string,int,float,bool
1 //... 操作符: 表示這是一個(gè)可變參數(shù). php5.6及以上的版本可使用: 函數(shù)定義的時(shí)候變量前使用. 2 function intSum(int ...$ints){ 3 return array_sum($ints); 4 } 5 var_dump(intSum(2,'3.5'));//5 6 7 //嚴(yán)格模式 8 //模式聲明:declare(strict_types=1); 默認(rèn)情況值為0,值為1代表為嚴(yán)格校驗(yàn)的模式 9 declare(strict_types=1);10 function add(int $a,int $b){11 return $a+$b;12 }13 var_dump(add(2,'3.5')); //Fatal error: Uncaught TypeError: Argument 2 passed to add() must be of the type integer
5. 返回值類型聲明
增加返回類型聲明的支持.類似于參數(shù)類型聲明.(用法在函數(shù)定義的后面加 :類型名)
1 //有效的返回類型2 declare(strict_types = 1);3 function getInt(int $value): int {4 return $value;5 }6 print(getInt(6));//6
1 //無效返回類型2 declare(strict_types = 1);3 function getNoInt(int $value): int {4 return $value+'2.5';5 }6 print(getNoInt(6));//Fatal error: Uncaught TypeError: Return value of getNoInt() must be of the type integer
6. 匿名類
允許new class {} 創(chuàng)建一個(gè)匿名的對象.
1 <?php 2 //php7以前 接口實(shí)現(xiàn) 3 interface User{ 4 public function getDiscount(); 5 } 6 class VipUser implements User{ 7 //折扣系數(shù) 8 private $discount = 0.6; 9 public function getDiscount() {10 return $this->discount;11 }12 }13 class Goods{14 private $price = 200;15 private $objectVipUser;16 //User接口VipUser類實(shí)現(xiàn)17 public function getUserData($User){18 $this->objectVipUser = $User;19 $discount = $this->objectVipUser->getDiscount();20 echo "商品價(jià)格:".$this->price*$discount;21 }22 }23 $display = new Goods();24 //常規(guī)實(shí)例化接口實(shí)現(xiàn)對象25 $display ->getUserData(new VipUser);//商品價(jià)格:120
1 <?php 2 //php7 創(chuàng)建一個(gè)匿名的對象 3 interface User{ 4 public function getDiscount(); 5 } 6 class Goods{ 7 private $price = 200; 8 private $objectVipUser; 9 public function getUserData($User){10 $this->objectVipUser = $User;11 $discount = $this->objectVipUser->getDiscount();12 echo "商品價(jià)格:".$this->price*$discount;13 }14 }15 $display = new Goods();16 //new匿名對象實(shí)現(xiàn)user接口17 $display ->getUserData(new class implements User{18 private $discount = 0.6;19 public function getDiscount() {20 return $this->discount;21 }22 });//商品價(jià)格:120
7. Closure::call()
Closure::call() 方法被添加為一個(gè)簡短的方式來臨時(shí)綁定一個(gè)對象作用域到一個(gè)閉包并調(diào)用它. 與PHP5的bindTo相比.它的性能要快得多.
1 <?php 2 //php7以前 3 class A { 4 private $attribute = 'hello world'; 5 } 6 7 $getClosure = function(){ 8 return $this->attribute; 9 };10 11 $getAttribute = $getClosure->bindTo(new A, 'A');//中間層閉包12 echo $getAttribute();//hello world
1 <?php 2 //PHP7 3 class A { 4 private $attribute = 'hello world'; 5 } 6 7 $getClosure = function(){ 8 return $this->attribute; 9 };10 11 echo $getClosure->call(new A);//hello world
8. unserialize()
unserialize()函數(shù):過濾的特性,可以防止非法數(shù)據(jù)進(jìn)行代碼注入,提供了更安全的反序列化數(shù)據(jù)
1 <?php 2 class A{ 3 public $name = 'admin_a'; 4 } 5 class B{ 6 public $name = 'admin_b'; 7 } 8 9 $objA = new A(); 10 $objB = new B(); 11 12 $serializedObjA = serialize($objA); 13 $serializedObjB = serialize($objB); 14 15 16 //默認(rèn)行為是接收所有類; 第二個(gè)參數(shù)可以忽略17 $dataA = unserialize($serializedObjA , ["allowed_classes" => true]); 18 var_dump($dataA);//object(A)#3 (1) { ["name"]=> string(7) "admin_a" }19 20 //如果allowed_classes設(shè)置為false,unserialize會將所有對象轉(zhuǎn)換為__PHP_Incomplete_Class對象 21 $dataA = unserialize($serializedObjA , ["allowed_classes" => false]); 22 var_dump($dataA);//object(__PHP_Incomplete_Class)#4 (2) { ["__PHP_Incomplete_Class_Name"]=> string(1) "A" ["name"]=> string(7) "admin_a" }23 24 //轉(zhuǎn)換所有對象到 __PHP_Incomplete_Class對象,除了對象"B"25 $dataB = unserialize($serializedObjB , ["allowed_classes" => ["B"]]); 26 var_dump($dataB);//object(B)#3 (1) { ["name"]=> string(7) "admin_b" }
9. IntlChar
IntlChar:提供了一些可用于訪問Unicode字符信息的實(shí)用方法的訪問. 注意:必須安裝Intl擴(kuò)展才能使用!
1 var_dump(IntlChar::CODEPOINT_MAX);//int(1114111) 2 echo '<br>';3 var_dump(IntlChar::charName('+'));//string(9) "PLUS SIGN" 4 echo '<br>';5 var_dump(IntlChar::ispunct('?'));//bool(true)
10. CSPRNG
CSPRNG 函數(shù)提供一種簡單的機(jī)制來生成密碼的隨機(jī)數(shù).
random_bytes() -加密生存被保護(hù)的偽隨機(jī)字符串.
random_int() -加密生存被保護(hù)的偽隨機(jī)整數(shù).
1 $bytes = random_bytes(8); 2 echo(bin2hex($bytes));//隨機(jī)2073a110a2e3c4973 echo '<br>';4 echo(random_int(1, 999));//隨機(jī)7865 echo '<br>';6 print(random_int(-999, -1));//隨機(jī)-357
11. use 語句
可以使用單個(gè)use語句從相同的命名空間導(dǎo)入類,函數(shù)和常量,而不是使用多個(gè)use語句.
1 //PHP7之前 2 use some\namespace\ClassA; 3 use some\namespace\ClassB; 4 use some\namespace\ClassC as C; 5 use function some\namespace\fn_a; 6 use function some\namespace\fn_b; 7 use function some\namespace\fn_c; 8 use const some\namespace\ConstA; 9 use const some\namespace\ConstB;10 use const some\namespace\ConstC;11 12 // PHP7之后13 use some\namespace\{ClassA, ClassB, ClassC as C};14 use function some\namespace\{fn_a, fn_b, fn_c};15 use const some\namespace\{ConstA, ConstB, ConstC};
12. intp
新增加intp()函數(shù),接收兩個(gè)參數(shù),返回值為第一個(gè)參數(shù)除于第二個(gè)參數(shù)的值并取整.
1 echo intp(8,4);//22 echo intp(10,4);//23 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塊所捕獲. 如果沒有匹配的try / catch塊,則調(diào)用異常處理函數(shù)(由 set_exception_handler() 注冊)進(jìn)行處理. 如果尚未注冊異常處理函數(shù),則按照傳統(tǒng)方式處理:被報(bào)告為一個(gè)致命錯(cuò)誤(Fatal Error).
Error類并不是從Exception類擴(kuò)展出來的,所以用catch (Exception $e) { ... } 這樣的代碼是捕獲不到Error的.你可以用 catch (Error $e) { ... } 這樣的代碼, 或者通過注冊異常處理函數(shù)( set_exception_handler())來捕獲Error.
1 <?php 2 //php7以前 自定義異常處理 3 class getException extends Exception{ 4 public function errorMsg(){ 5 return '錯(cuò)誤的信息'.$this->getMessage().'<br>錯(cuò)誤的代碼'.$this->getCode(); 6 } 7 } 8 9 try {10 $num =10;11 if($num > 1) {12 throw new getException($num,404);13 }14 } catch (getException $e) {15 echo $e->errorMsg();16 }
1 <?php 2 //php7 異常處理3 try {4 test();5 }catch(Error $e) {6 echo $e->getMessage();//Call to undefined function test()7 }
感謝各位的閱讀!關(guān)于“php7新特性的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。