久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      聊聊php7新特性的理解和比較

      聊聊php7新特性的理解和比較

      null合并運算符(??)

      ??語法: 如果變量存在且值不為NULL,它就會返回自身的值,否則返回它的第二個操作數(shù).

      //php7以前  if判斷if(empty($_GET['param'])) {       $param = 1; }else{     $param = $_GET['param']; }//php7以前  三元運算符$param = empty($_GET['param']) ? 1 : $_GET['param'];//PHP7  null合并運算符$param = $_GET['param'] ?? 1;//1

      define() 定義常量數(shù)組

      //php7以前define("CONTENT", "hello world");echo CONTENT;//hello world//PHP7define('ANIMALS', [    'dog',    'cat',    'bird']);echo ANIMALS[2];//bird//PHP7 類外也可使用const來定義常量const CONSTANT = 'Hello World';  echo CONSTANT;//Hello World

      組合比較符(<=>)

      組合比較符用于比較兩個表達式.當聊聊php7新特性的理解和比較b時它分別返回-1、0或1. 比較的原則是沿用PHP的常規(guī)比較規(guī)則進行的.

      //整數(shù)echo 1 <=> 1; // 0echo 1 <=> 2; // -1echo 2 <=> 1; // 1//浮點數(shù)echo 1.5 <=> 1.5; // 0echo 1.5 <=> 2.5; // -1echo 2.5 <=> 1.5; // 1  //字符串echo "a" <=> "a"; // 0echo "a" <=> "b"; // -1echo "b" <=> "a"; // 1

      變量類型聲明

      兩種模式: 強制(默認)和嚴格模式. 可以使用下列類型參數(shù): string,int,float,bool

      //... 操作符: 表示這是一個可變參數(shù). php5.6及以上的版本可使用: 函數(shù)定義的時候變量前使用.function intSum(int ...$ints){    return array_sum($ints); } var_dump(intSum(2,'3.5'));//5//嚴格模式//模式聲明:declare(strict_types=1);  默認情況值為0,值為1代表為嚴格校驗的模式 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

      返回值類型聲明

      增加返回類型聲明的支持.類似于參數(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

      匿名類

      允許new class {} 創(chuàng)建一個匿名的對象.

      <?php//php7以前 接口實現(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類實現(xiàn)     public function getUserData(User $User){        $this->objectVipUser = $User;         $discount = $this->objectVipUser->getDiscount();        echo "商品價格:".$this->price*$discount;     } } $display = new Goods();//常規(guī)實例化接口實現(xiàn)對象$display->getUserData(new VipUser);//商品價格:120
      <?php//php7 創(chuàng)建一個匿名的對象interface User{    public function getDiscount(); }class Goods{    private $price = 200;    private $objectVipUser;    public function getUserData($User){        $this->objectVipUser = $User;         $discount = $this->objectVipUser->getDiscount();        echo "商品價格:".$this->price*$discount;     } } $display = new Goods();//new匿名對象實現(xiàn)user接口$display->getUserData(new class implements User{    private $discount = 0.6;    public function getDiscount() {        return $this->discount;     } });//商品價格:120

      Closure::call()

      Closure::call() 方法被添加為一個簡短的方式來臨時綁定一個對象作用域到一個閉包并調(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//PHP7class A {    private  $attribute = 'hello world'; }  $getClosure = function(){    return $this->attribute; };echo $getClosure->call(new A);//hello world

      unserialize()

      unserialize()函數(shù):過濾的特性,可以防止非法數(shù)據(jù)進行代碼注入,提供了更安全的反序列化數(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);    //默認行為是接收所有類; 第二個參數(shù)可以忽略$dataA = unserialize($serializedObjA , ["allowed_classes" => true]);  var_dump($dataA);//object(A)#3 (1) { ["name"]=> string(7) "admin_a" }//如果allowed_classes設(shè)置為false,unserialize會將所有對象轉(zhuǎn)換為__PHP_Incomplete_Class對象 $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)換所有對象到 __PHP_Incomplete_Class對象,除了對象"B"$dataB = unserialize($serializedObjB , ["allowed_classes" => ["B"]]);  var_dump($dataB);//object(B)#3 (1) { ["name"]=> string(7) "admin_b" }

      IntlChar

      IntlChar:提供了一些可用于訪問Unicode字符信息的實用方法的訪問. 注意:必須安裝Intl擴展才能使用!

      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)

      CSPRNG

      CSPRNG 函數(shù)提供一種簡單的機制來生成密碼的隨機數(shù).

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

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

      1 $bytes = random_bytes(8);  2 echo(bin2hex($bytes));//隨機2073a110a2e3c4973 echo '<br>';4 echo(random_int(1, 999));//隨機7865 echo '<br>';6 print(random_int(-999, -1));//隨機-357

      use 語句

      可以使用單個use語句從相同的命名空間導入類,函數(shù)和常量,而不是使用多個use語句.

      //PHP7之前use somenamespaceClassA;use somenamespaceClassB;use somenamespaceClassC as C;use function somenamespacefn_a;use function somenamespacefn_b;use function somenamespacefn_c;use const somenamespaceConstA;use const somenamespaceConstB;use const somenamespaceConstC;// PHP7之后use somenamespace{ClassA, ClassB, ClassC as C};use function somenamespace{fn_a, fn_b, fn_c};use const somenamespace{ConstA, ConstB, ConstC};

      intp

      新增加intp()函數(shù),接收兩個參數(shù),返回值為第一個參數(shù)除于第二個參數(shù)的值并取整.

      1 echo intp(8,4);//22 echo intp(10,4);//23 echo intp(5,10);//0

      PHP7 錯誤處理

      PHP7 改變了大多數(shù)錯誤的報告方式.不同于PHP5的傳統(tǒng)錯誤報告機制,現(xiàn)在大多數(shù)錯誤被作為Error異常拋出.
      這種Error異??梢韵衿胀ó惓R粯颖籺ry / catch塊所捕獲. 如果沒有匹配的try / catch塊,則調(diào)用異常處理函數(shù)(由 set_exception_handler() 注冊)進行處理.
      如果尚未注冊異常處理函數(shù),則按照傳統(tǒng)方式處理:被報告為一個致命錯誤(Fatal Error).
      Error類并不是從Exception類擴展出來的,所以用catch (Exception $e) { … } 這樣的代碼是捕獲不到Error的.你可以用 catch (Error $e) { … } 這樣的代碼,
      或者通過注冊異常處理函數(shù)( set_exception_handler())來捕獲Error.

      聊聊php7新特性的理解和比較

      <?php//php7以前 自定義異常處理class getException extends Exception{    public function errorMsg(){        return '錯誤的信息'.$this->getMessage().'<br>錯誤的代碼'.$this->getCode();     } }try {     $num =10;    if($num > 1) {        throw new getException($num,404);     } } catch (getException $e) {    echo $e->errorMsg(); }
      <?php  //php7 異常處理try {     test(); }catch(Exception $e){    echo $e->getMessage();//自定義異常拋出}catch(Error $e) {  //系統(tǒng)錯誤     echo $e->getMessage();//Call to undefined function test()}

      推薦教程:《PHP教程》

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號