PHP7 新特性 学习
PHP7 的新特性大概浏览下,还是能在工作的效率上有很大益处的。
1,性能提升
这个我就不做测试了,哈哈
2,类型声明
1 | class Student |
3,可以声明严格类型校验模式 , 此声明必须第一个声明
1 | declare (strict_types = 1); |
4, 标量类型提示
1 | function getTotal(float $a, float $b) |
5, 返回类型声明
1 | function getSum(float $a, float $b): int |
6, 错误处理
新的继承如下
|- Exception implements Throwable
|- …
|- Error implements Throwable
|- TypeError extends Error
|- ParseError extends Error
|- ArithmeticError extends Error
|- DivisionByZeroError extends ArithmeticError
|- AssertionError extends Error
1 | try { |
7, Null Coalesce Operator
1 | $name = $firstName ?? "Guest"; |
等同于
1 | if (!empty($firstName)) { |
还可以像下面这样使用
1 | $name = $firstName ?? $username ?? $placeholder ?? “Guest”; |
8, Spaceship Operator
1 | $compare = 2 <=> 1 |
等同于下面
1 | 2 < 1? return -1 |
9,Easy User-land CSPRNG: random_int and random_bytes.
1 | $int = random_int(1, 2); |