当前位置:网站首页 >> php教程 >> php7新特性的理解和比较
php7新特性的理解和比较

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

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

//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

2. define() 定义常量数组

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

3. 组合比较符(<=>)

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

/整数 
echo 1 <=> 1; // 0 
echo 1 <=> 2; // -1 
echo 2 <=> 1; // 1 
 
 //浮点数 
echo 1.5 <=> 1.5; // 0 
echo 1.5 <=> 2.5; // -1 
echo 2.5 <=> 1.5; // 1
  
  //字符串
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1

4. 变量类型声明

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

//... 操作符: 表示这是一个可变参数. php5.6及以上的版本可使用: 函数定义的时候变量前使用. 
  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

5. 返回值类型声明

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

//有效的返回类型
declare(strict_types = 1);
 function getInt(int $value): int {
   return $value;
 }
 print(getInt(6));//6
//无效返回类型
declare(strict_types = 1);
 function getNoInt(int $value): int {
   return $value+'2.5';
 }
 print(getNoInt(6));//Fatal error: Uncaught TypeError: Return value of getNoInt() must be of the type integer

6. 匿名类

允许new class {} 创建一个匿名的对象.

//php7以前 接口实现 
  interface User{ 
      public function getDiscount(); 
  } 
  class VipUser implements User{ 
      //折扣系数 
      private $discount = 0.6; 
      public function getDiscount() {
          return $this->discount;
      }
  }
  class Goods{
      private $price = 200;
      private $objectVipUser;
      //User接口VipUser类实现
      public function getUserData($User){
          $this->objectVipUser = $User;
          $discount = $this->objectVipUser->getDiscount();
          echo "商品价格:".$this->price*$discount;
      }
  }
  $display = new Goods();
  //常规实例化接口实现对象
  $display ->getUserData(new VipUser);//商品价格:120
//php7 创建一个匿名的对象 
  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匿名对象实现user接口
  $display ->getUserData(new class implements User{
      private $discount = 0.6;
      public function getDiscount() {
          return $this->discount;
      }
  });//商品价格:120


  • jQuery网页特效,PHP源码下载,最新网页模板和网站模板,jQuery代码,前端教程-35素材网
  • 35素材版权所有www.35sucai.comInc. All Rights Reserved.  
  • 本网站提供的所有资源均来自互联网搜集,作品版权为原作者所有。
  • 如本站有侵害到您权益的资源请来信告知我们,我们会立即删除侵害到您权益的内容。
  • 站长邮箱:lei1155@qq.com    粤ICP备18080170号