1、可以使用表达式定义常量
<?php
const ONE = 1 * 1;
class A {
  const TWO = 1 * 2;
  const THREE = self::TWO + 1;
  
  public function test($i = self::THREE + ONE) {
    echo $i;
  }
}
(new A())->test();
2、使用...定义变长函数参数
<?php
function total(...$nums) {
  $total = 0;
  foreach($nums as $num) {
    $total += $num;
  }
  return $total;
}
echo total(1, 2, 3, 4, 5);
$arr = [3, 4, 5, 6];
echo total(...$arr);
3、使用**进行幂运算
<?php echo 2 ** 4; $a = 2; $a **= 4; echo $a;
4、use function和use const
<?php
namespace A {
  const PI = 3.14;
  function test() {
    echo 'test';
  }
}
namespace B {
  use function \A\test;
  use const \A\PI;
  
  echo PI;
  test();
}
5、加入hash_equals()函数,以恒定的时间消耗来进行字符串比较,以避免时序攻击。
6、加入__debugInfo()
当使用var_dump()输出对象的时候,可以用来控制要输出的属性和值。
<?php
class A {
  protected $a = 1;
  protected $b = 2;
  protected $c = 3;
  public function __debugInfo() {
    //返回值必须是数组
    return array(
      'a' => $this->a,
    );
  }
}
var_dump((new A()));
五、php7新增的特性
1、??运算符(NULL合并运算符)
<?php $page = $_GET['page'] ?? 1;
2、标量类型声明
3、函数返回值类型声明
<?php
declare(strict_types=1);
function add(int $num1, int $num2) : int {
  return $num1 + $num2;
}
echo add(2, 3);
//在严格模式下,下面这行会报错
echo add(2.0, 3.0);
4、匿名类
<?php
(new class {
  public function test() {
    echo 'test';
  }
})->test();
5、通过define()定义常量数组
<?php
define('ARR', ['a', 'b', 'c']);
echo ARR[2];
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
