PHP7新特性介紹
文內容根據PHP發布時的 new files 而來,鏈接地址 : PHP 7 new
特性一覽
- Added??operator
- Added <=> operato
新的操作符
</ul>
// PHP 7之前的寫法:比較兩個數的大小 function order_func($a, $b) { return ($a < $b) ? -1 : (($a > $b) ? 1 : 0); } // PHP新增的操作符 <=>,perfect function order_func($a, $b) { return $a <=> $b; }
- Added\u{xxxxx}Unicode Codepoint Escape Syntax
可以直接使用這種方式輸出unicode字符
</ul>
echo "\u{1F602}"; // outputs (這個是方括號里面的問號,因為是unicode字符,sg博客不支持,發布出來)
-
Added error_clear_last() function
</li>
新增的一個函數,具體功能沒有深入研究 -
Implemented the RFCScalar Type Decalarations v0.5. (Anthony)
變量類型聲明(int, float, string , bool)
與之相關的一個語法:declare(strict_types=1);
當strict_types 設定為0,PHP會對函數參數和返回值進行嚴格的類型判斷
需要主要的是1declare(strict_types=1);必須放在PHP文件的第一行,此行不能包含其他內容
</blockquote> </li> </ul>
2declare(strict_types=1);只會影響文件內部,包含此聲明文件的文件不會影響
3 判斷一個函數或者方法是強類型判斷還是弱類型判斷,就看聲明函數或者方法的文件,在開始的時候
是否有declare(strict_types=1);,有這一句,就是強類型
語法演示//聲明函數返回值類型的寫法和參數類型 function foobar(float $abc): int { return ceil($abc + 1); }
這里聲明了方法參數的類型,當調用的時候,如果不是相應的類型,會先嘗試進行類型轉換,然后把轉換后的值傳入
- mplemented the RFCGroup Use Declarations. (Marcio)
這個沒有什么可說的,PHP use引入類更加方便整齊
</ul>
// 新語法寫法
use FooLibrary\Bar\Baz{ ClassA, ClassB, ClassC, ClassD as Fizbo };
// 以前語法的寫法
use FooLibrary\Bar\Baz\ClassA; use FooLibrary\Bar\Baz\ClassB; use FooLibrary\Bar\Baz\ClassC; use FooLibrary\Bar\Baz\ClassD as Fizbo;</pre>
-
Implemented the RFCContinue Output Buffering. (Mike)
</li>
//TODO -
Implemented the RFCConstructor behaviour of internal classes. (Dan, Dmitry)
</li>
//TODO -
Implemented the RFCFix "foreach" behavior. (Dmitry)
</li>
foreach 語法的一些邊界添加處理,
https://wiki.php.net/rfc/php7_foreach -
Implemented the RFCGenerator Delegation. (Bob)
</li> </ul>
增強了Generator的功能,這個可以實現很多先進的特性<?php //牛逼的用法,輕量級的線程 function g() { yield 1; yield from [2, 3, 4]; yield 5; }
$g = g(); foreach ($g as $yielded) { var_dump($yielded); }
/ int(1) int(2) int(3) int(4) int(5)/</pre>
-
Implemented the RFCAnonymous Class Support. (Joe, Nikita, Dmitry)
</li>
匿名類,這個就不具體介紹了 -
Implemented the RFCContext Sensitive Lexer. (Marcio Almada)
</li> </ul> 來自:http://segmentfault.com/a/1190000002921671
這個特性主要是PHP的面向對象特性更加友好
在class里面類屬性或者方法可以使用一些關鍵字比如 foreach,list,for等
-
- mplemented the RFCGroup Use Declarations. (Marcio)