搜索
首页后端开发PHP7理解和比较php7新特性

理解和比较php7新特性

推荐(免费):PHP7

1、 null合并运算符(??)

??语法: 如果变量存在且值不为NULL,它就会返回自身的值,否则返回它的第二个操作数.

 1 //php7以前  
 if判断 
 2 if(empty($_GET['param'])) { 
 3       $param = 1; 
 4 }else{ 
 5     $param = $_GET['param']; 
 6 } 7  
 8 //php7以前  三元运算符 
 9 $param = empty($_GET['param']) ? 1 : $_GET['param'];10 
 11 //PHP7  null合并运算符12 $param = $_GET['param'] ?? 1;//1

2. define() 定义常量数组

 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. 组合比较符(<=>)

组合比较符用于比较两个表达式.当$a小于、等于或大于$b时它分别返回-1、0或1. 比较的原则是沿用PHP的常规比较规则进行的.

 1 //整数 
 2 echo 1 <=> 1; // 0 
 3 echo 1 <=> 2; // -1 
 4 echo 2 <=> 1; // 1 5  
 6 //浮点数 7 echo 1.5 <=> 1.5; // 0 
 8 echo 1.5 <=> 2.5; // -1 
 9 echo 2.5 <=> 1.5; // 1 
 11 //字符串12 echo "a" <=> "a"; // 0
 13 echo "a" <=> "b"; // -1
 14 echo "b" <=> "a"; // 1

4. 变量类型声明

两种模式: 强制(默认)和严格模式. 可以使用下列类型参数: string,int,float,bool

 1 //... 操作符: 表示这是一个可变参数. php5.6及以上的版本可使用: 函数定义的时候变量前使用. 
 2 function intSum(int ...$ints){ 
 3     return array_sum($ints); 
 4 } 
 5 var_dump(intSum(2,'3.5'));//5 
 6  
 7 //严格模式 
 8 //模式声明:declare(strict_types=1);  默认情况值为0,值为1代表为严格校验的模式  
 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. 返回值类型声明

增加返回类型声明的支持.类似于参数类型声明.(用法在函数定义的后面加 :类型名)

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 {} 创建一个匿名的对象.

 1 <?php 
 2 //php7以前 接口实现 
 3 interface User{ 
 4     public function getDiscount(); 
 5 } 
 6 class VipUser implements User{ 
 7     //折扣系数 
 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类实现
 17     public function getUserData($User){
 18         $this->objectVipUser = $User;
 19         $discount = $this->objectVipUser->getDiscount();
 20         echo "商品价格:".$this->price*$discount;
 21     }
 22 }
 23 $display = new Goods();
 24 //常规实例化接口实现对象
 25 $display ->getUserData(new VipUser);//商品价格:120
 1 <?php 
 2 //php7 创建一个匿名的对象 
 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 "商品价格:".$this->price*$discount;
 13     }
 14 }
 15 $display = new Goods();
 16 //new匿名对象实现user接口
 17 $display ->getUserData(new class implements User{
 18     private $discount = 0.6;
 19     public function getDiscount() {
 20         return $this->discount;
 21     }
 22 });//商品价格:120

7. Closure::call()

Closure::call() 方法被添加为一个简短的方式来临时绑定一个对象作用域到一个闭包并调用它. 与PHP5的bindTo相比.它的性能要快得多.

 1 <?php 
 2 //php7以前 
 3 class A { 
 4     private  $attribute = &#39;hello world&#39;; 
 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 = &#39;hello world&#39;; 
 5 } 
 6  
 7 $getClosure = function(){ 
 8     return $this->attribute; 
 9 };
 10 
 11 echo $getClosure->call(new A);//hello world

8. unserialize()

unserialize()函数:过滤的特性,可以防止非法数据进行代码注入,提供了更安全的反序列化数据

 1 <?php 
 2 class A{  
 3    public $name = &#39;admin_a&#39;;    
 4 } 
 5 class B{ 
 6    public $name = &#39;admin_b&#39;; 
 7 } 
 8  9 $objA = new A(); 
10 $objB = new B(); 
11 12 $serializedObjA = serialize($objA); 
13 $serializedObjB = serialize($objB); 
14 15 16 //默认行为是接收所有类; 第二个参数可以忽略17 $dataA = unserialize($serializedObjA , ["allowed_classes" => true]); 
18 var_dump($dataA);//object(A)#3 (1) { ["name"]=> string(7) "admin_a" }19 20 //如果allowed_classes设置为false,unserialize会将所有对象转换为__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 //转换所有对象到 __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字符信息的实用方法的访问. 注意:必须安装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)

10. CSPRNG

CSPRNG 函数提供一种简单的机制来生成密码的随机数.

random_bytes() -加密生存被保护的伪随机字符串.

random_int() -加密生存被保护的伪随机整数.

1 $bytes = random_bytes(8); 
2 echo(bin2hex($bytes));//随机2073a110a2e3c497
3 echo '<br>';
4 echo(random_int(1, 999));//随机786
5 echo '<br>';
6 print(random_int(-999, -1));//随机-357

11. use 语句

可以使用单个use语句从相同的命名空间导入类,函数和常量,而不是使用多个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()函数,接收两个参数,返回值为第一个参数除于第二个参数的值并取整.

1 echo intp(8,4);//2
2 echo intp(10,4);//2
3 echo intp(5,10);//0

13. PHP7 错误处理

PHP7 改变了大多数错误的报告方式.不同于PHP5的传统错误报告机制,现在大多数错误被作为Error异常抛出.
这种Error异常可以像普通异常一样被try / catch块所捕获. 如果没有匹配的try / catch块,则调用异常处理函数(由 set_exception_handler() 注册)进行处理.
如果尚未注册异常处理函数,则按照传统方式处理:被报告为一个致命错误(Fatal Error).
Error类并不是从Exception类扩展出来的,所以用catch (Exception $e) { ... } 这样的代码是捕获不到Error的.你可以用 catch (Error $e) { ... } 这样的代码,
或者通过注册异常处理函数( set_exception_handler())来捕获Error.

 1 <?php 
 2 //php7以前 自定义异常处理 
 3 class getException extends Exception{ 
 4     public function errorMsg(){ 
 5         return &#39;错误的信息&#39;.$this->getMessage().'<br>错误的代码'.$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 }

以上是理解和比较php7新特性的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:CSDN。如有侵权,请联系admin@php.cn删除

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)