php7新特性一覽

1.太空船操做符

用於比較2個表達式,例如當\(a小於,等於或大於\)b時,分別返回-1,0,1
php echo 1 <=> 1; //0 echo PHP_EOL; echo 1 <=> 2; //-1 echo PHP_EOL; echo 2 <=> 1; //1php

2.標量類型和返回值類型聲明

  • php7可對字符串(string),整型(int),浮點以及布爾類型的參數作聲明。
  • 參數類型聲明不受制於默認模式和嚴格模式,默認模式下,當傳入的參數不符合聲明類型時,會首先嚐試類型轉換(這裏的類型轉換僅僅適用於可轉換的類型,把'a'轉換爲Int也會報錯),而嚴格模式下則直接報錯
declare(strict_types=1); //strict_types=1表示嚴格模式
function sum(int ...$ints){
    return array_sum($ints);
}
var_dump(sum(1,'2','3.1',4.1));
執行結果
Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given
  • 若是關閉嚴格模式,傳入的參數會先轉換爲Int,結果爲10web

    返回值類型受限制的

    下面代碼聲明返回值爲int
declare(strict_types=1); 
function sum(int ...$ints):int
{
    return array_sum($ints);
}
var_dump(sum(1,2,3)); //6

php7.1對函數返回值類型作了擴充,可定義返回值類型爲void,不管是否開啓嚴格模式,==只要函數有"return;" 之外的其它return語句都會報錯==數組

declare(strict_types=1); 
function sum(int ...$ints):void
{
    // return NULL; //報錯
    return ; //後面有 "return;"以外的語句也報錯 
    // return array_sum($ints);
}
var_dump(sum(1,2,3));

php7.1對參數類型和返回值類型還有進一步的支持,其類型可使空類型,在參數或者返回值類型聲明前加上"?"表示要麼是null,要麼是聲明的類型php7

declare(strict_types=1); 
function test(?int $a):?int
{
    return $a;
}
var_dump(test(null)); //NULL 
var_dump(test(1));  //int(1)
var_dump(test('a'));  // Fatal error

3.null合併操做符

$page=isset($_GET['p'])?$_GET['p']:0;
//php7
$page =$_GET['p'] ??0;

//三元運算符
$page =$_GET['p'] ?? $_POST['p'] ??0;

4.常量數組

php7以前沒法經過define來定義一個常量數組的,php7支持了這個操做閉包

define('STUDENT',['boy','girl']);
print_r(STUDENT);

5.namespace批量導入

//php以前
use Space\ClassA;
use Space\ClassB;
use Space\ClassC C;

//php7
use Space\{ClassA,ClassB,ClassC as C};

6.throwable接口

  • 在php7以前,若是代碼中有語法錯誤或者fatal error時,程序會直接報錯退出,php7中實現了全局throwable接口,原來的Exception和部分Error實現了該接口函數

  • 這種Error能夠像Exception同樣被第一個匹配的try/catch塊捕獲。若是沒有匹配的catch塊,則調用異常函數處理。若是還沒有註冊異常處理函數,則按照傳統方式處理(Fatal error)this

  • Error類並不是繼承自Exception,因此不能用catch(Exception $e){}來捕獲,可用catch(Error $e){} ,或者經過註冊異常處理函數(set_exception_handler())來捕獲Errorspa

try{
    abc();
}catch (Error $e){
    print_r($e);
}
echo 123;
php5裏 Fatal error ,當即退出 以後的代碼不會執行了
php7裏
Error Object
(
    [message:protected] => Call to undefined function abc()
    [string:Error:private] => 
    [code:protected] => 0
    [file:protected] => /mnt/hgfs/www/web/md.php
    [line:protected] => 4
    [trace:Error:private] => Array
        (
        )

    [previous:Error:private] => 
)
123

或者經過註冊異常處理函數來處理

set_exception_handler(function($e){
    echo "err:".print_r($e,true);
    
});

abc();
echo 123;

php7下結果,php5依然Fatal error指針

err:Error Object
(
    [message:protected] => Call to undefined function abc()
    [string:Error:private] => 
    [code:protected] => 0
    [file:protected] => /mnt/hgfs/www/web/md.php
    [line:protected] => 7
    [trace:Error:private] => Array
        (
        )

    [previous:Error:private] => 
)
以後的也不會執行了

7.Closure::call()

在php7以前,當動態的給一個對象添加方法時,能夠經過Closure來複制一個閉包對象,並綁定到一個$this對象和類做用域

class People{
    private $age=10;
}
$f=function(){
    return $this->age+1;
};

$p=$f->bindTo(new People,'People');
echo $p();

在php7可經過call來暫時綁定一個閉包對象到$this對象並調用它

class People{
    private $age=10;
}
$f=function(){
    return $this->age+1;
};

echo $f->call(new People);

8.intdiv

echo intdiv(10,3);

9.list方括號寫法

$a=[1,2,3];
list($n1,$n2,$n3)=$a;

//php7
[$n1,$n2,$n3]=[4,5,6]; //[]並非數組,而是list的簡略形式

10.foreach遍歷數組再也不修改內部指針

$arr=[1,2,3,4,5,6];
foreach ($arr as $key => $value) {
    if($value ==2) break;
}
echo current($arr);//php7下 1,php5下 3

11.匿名類可使用 new class來定義,匿名類可使用來代替完整的定義

interface Cache{
    public function read ();
}
class Ca {
    private $cache;
    public function setcache(Cache $cache){
        $this->cache=$cache;
    }

}
$fcache =new Ca;
$fcache->setcache(new Class implements Cache {
    public function read(){

    }
});

其它,移除ASP和script PHP標籤,移除$HTTP_RAW_POST_DATA、匿名類、常量類可見性等code

相關文章
相關標籤/搜索