本篇文章介绍了关于php5.5 ~ php7.2 新特性整理,有需要的朋友可以参考一下
从php 5.5.x 移植到 php 5.6.x新特性使用表达式定义常量在之前的 php 版本中, 必须使用静态值来定义常量,声明属性以及指定函数参数默认值。 现在你可以使用包括数值、字符串字面量以及其他常量在内的数值表达式来 定义常量、声明属性以及设置函数参数默认值。
<?php
const one = 1;
const two = one * 2;
class c {
const three = two + 1;
const one_third = one / self::three;
const sentence = 'the value of three is '.self::three;
}
现在可以通过 const 关键字来定义类型为 array 的常量。
<?php
const arr = ['a', 'b'];
echo arr[0];
使用 ... 运算符定义变长参数函数现在可以不依赖 func_get_args(), 使用 ... 运算符 来实现 变长参数函数。
<?php
function f($req, $opt = null, ...$params) {
// $params 是一个包含了剩余参数的数组
printf('$req: %d; $opt: %d; number of params: %d'."\n",
$req, $opt, count($params));
}
f(1);
f(1, 2);
f(1, 2, 3);
f(1, 2, 3, 4);
?>
以上例程会输出:
$req: 1; $opt: 0; number of params: 0
$req: 1; $opt: 2; number of params: 0
$req: 1; $opt: 2; number of params: 1
$req: 1; $opt: 2; number of params: 2
使用 ... 运算符进行参数展开在调用函数的时候,使用 ... 运算符, 将 数组 和 可遍历 对象展开为函数参数。 在其他编程语言,比如 ruby中,这被称为连接运算符。
<?php
function add($a, $b, $c) {
return $a + $b + $c;
}
$operators = [2, 3];
echo add(1, ...$operators);
?>
以上例程会输出:
6
use function 以及 use constuse 运算符 被进行了扩展以支持在类中导入外部的函数和常量。 对应的结构为 use function 和 use const。
<?php
namespace name\space {
const foo = 42;
function f() { echo __function__."\n"; }
}
namespace {
use const name\space\foo;
use function name\space\f;
echo foo."\n";
f();
}
?>
以上例程会输出:
42
name\space\f
使用 hash_equals() 比较字符串避免时序攻击从php 5.6.x 移植到 php 7.0.x新特性标量类型声明标量类型声明 有两种模式: 强制 (默认) 和 严格模式。 现在可以使用下列类型参数(无论用强制模式还是严格模式): 字符串(string), 整数 (int), 浮点数 (float), 以及布尔值 (bool)。
<?php
// coercive mode
function sumofints(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumofints(2, '3', 4.1));
以上例程会输出:
int(9)
返回值类型声明php 7 增加了对返回类型声明的支持。 类似于参数类型声明,返回类型声明指明了函数返回值的类型。可用的类型与参数声明中可用的类型相同。
<?php
function arrayssum(array ...$arrays): array
{
return array_map(function(array $array): int {
return array_sum($array);
}, $arrays);
}
null合并运算符由于日常使用中存在大量同时使用三元表达式和 isset()的情况, 我们添加了null合并运算符 () 这个语法糖。如果变量存在且值不为null, 它就会返回自身的值,否则返回它的第二个操作数。
<?php
// fetches the value of $_get['user'] and returns 'nobody' if it does not exist.
$username = $_get['user'] 'nobody';
// this is equivalent to:
$username = isset($_get['user']) ? $_get['user'] : 'nobody';
// coalesces can be chained: this will return the first defined value out of $_get['user'], $_post['user'], and 'nobody'.
$username = $_get['user'] $_post['user'] 'nobody';
?>
太空船操作符(组合比较符)太空船操作符用于比较两个表达式。当$a小于、等于或大于$b时它分别返回-1、0或1。 比较的原则是沿用 php 的常规比较规则进行的。
<?php
// 整数
echo 1 <=> '1'; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
// 浮点数
echo '1.50' <=> 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
?>
通过 define() 定义常量数组array 类型的常量现在可以通过 define() 来定义。在 php5.6 中仅能通过 const 定义。
define('animals', [
'dog',
'cat',
'bird'
]);
echo animals[1]; // 输出 "cat"
closure::call()closure::call() 现在有着更好的性能,简短干练的暂时绑定一个方法到对象上闭包并调用它。
<?php
class a {private $x = 1;}
// php 7 之前版本的代码
$getxcb = function() {return $this->x;};
$getx = $getxcb->bindto(new a, 'a'); // 中间层闭包
echo $getx();
// php 7+ 及更高版本的代码
$getx = function() {return $this->x;};
echo $getx->call(new a);
以上例程会输出:
1
分组 use 声明从同一 namespace 导入的类、函数和常量现在可以通过单个 use 语句 一次性导入了。
<?php
// php 7 之前的代码
use some\namespace\classa;
use some\namespace\classb;
use some\namespace\classc as c;
use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;
use const some\namespace\consta;
use const some\namespace\constb;
use const some\namespace\constc;
// php 7+ 及更高版本的代码
use some\namespace\{classa, classb, classc as c};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{consta, constb, constc};
?>
生成器可以返回表达式此特性基于 php 5.5 版本中引入的生成器特性构建的。 它允许在生成器函数中通过使用 return 语法来返回一个表达式 (但是不允许返回引用值), 可以通过调用 generator::getreturn() 方法来获取生成器的返回值, 但是这个方法只能在生成器完成产生工作以后调用一次。
整数除法函数 intp()从php 7.0.x 移植到 php 7.1.x新特性可为空(nullable)类型参数以及返回值的类型现在可以通过在类型前加上一个问号使之允许为空。 当启用这个特性时,传入的参数或者函数返回的结果要么是给定的类型,要么是 null 。
<?php
function testreturn(): ?string
{
return 'elephpant';
}
var_dump(testreturn());
function testreturn(): ?string
{
return null;
}
var_dump(testreturn());
function test(?string $name)
{
var_dump($name);
}
test('elephpant');
test(null);
test();
以上例程会输出:
string(10) "elephpant"
null
string(10) "elephpant"
null
uncaught error: too few arguments to function test(), 0 passed in...
void 函数一个新的返回值类型void被引入。 返回值声明为 void 类型的方法要么干脆省去 return 语句,要么使用一个空的 return 语句。 对于 void 函数来说,null 不是一个合法的返回值。
<?php
function swap(&$left, &$right) : void
{
if ($left === $right) {
return;
}
$tmp = $left;
$left = $right;
$right = $tmp;
}
$a = 1;
$b = 2;
var_dump(swap($a, $b), $a, $b);
以上例程会输出:
null
int(2)
int(1)
symmetric array destructuring短数组语法([])现在作为list()语法的一个备选项,可以用于将数组的值赋给一些变量(包括在foreach中)。
<?php
$data = [
[1, 'tom'],
[2, 'fred'],
];
// list() style
list($id1, $name1) = $data[0];
// [] style
[$id1, $name1] = $data[0];
// list() style
foreach ($data as list($id, $name)) {
// logic here with $id and $name
}
// [] style
foreach ($data as [$id, $name]) {
// logic here with $id and $name
}
类常量可见性现在起支持设置类常量的可见性。
<?php
class constdemo
{
const public_const_a = 1;
public const public_const_b = 2;
protected const protected_const = 3;
private const private_const = 4;
}
iterable 伪类现在引入了一个新的被称为iterable的伪类 (与callable类似)。 这可以被用在参数或者返回值类型中,它代表接受数组或者实现了traversable接口的对象。 至于子类,当用作参数时,子类可以收紧父类的iterable类型到array 或一个实现了traversable的对象。对于返回值,子类可以拓宽父类的 array或对象返回值类型到iterable。
<?php
function iterator(iterable $iter) : iterable
{
foreach ($iter as $val) {
//
}
}
多异常捕获处理一个catch语句块现在可以通过管道字符(|)来实现多个异常的捕获。 这对于需要同时处理来自不同类的不同异常时很有用。
<?php
try {
// some code
} catch (firstexception | secondexception $e) {
// handle first and second exceptions
}
list()现在支持键名现在list()和它的新的[]语法支持在它内部去指定键名。这意味着它可以将任意类型的数组 都赋值给一些变量(与短数组语法类似)
<?php
$data = [
["id" => 1, "name" => 'tom'],
["id" => 2, "name" => 'fred'],
];
// list() style
list("id" => $id1, "name" => $name1) = $data[0];
// [] style
["id" => $id1, "name" => $name1] = $data[0];
// list() style
foreach ($data as list("id" => $id, "name" => $name)) {
// logic here with $id and $name
}
// [] style
foreach ($data as ["id" => $id, "name" => $name]) {
// logic here with $id and $name
}
从php 7.1.x 移植到 php 7.2.x新特性新的对象类型这种新的对象类型, object, 引进了可用于逆变(contravariant)参数输入和协变(covariant)返回任何对象类型。
<?php
function test(object $obj) : object
{
return new splqueue();
}
test(new stdclass());
允许重写抽象方法(abstract method)当一个抽象类继承于另外一个抽象类的时候,继承后的抽象类可以重写被继承的抽象类的抽象方法。
abstract class a
{
abstract function test(string $s);
}
abstract class b extends a
{
// overridden - still maintaining contravariance for parameters and covariance for return
abstract function test($s) : int;
}
扩展了参数类型重写方法和接口实现的参数类型现在可以省略了。不过这仍然是符合lsp,因为现在这种参数类型是逆变的。
interface a
{
public function test(array $input);
}
class b implements a
{
public function test($input){} // type omitted for $input
}
允许分组命名空间的尾部逗号命名空间可以在php 7中使用尾随逗号进行分组引入。
use foo\bar\{
foo,
bar,
baz,
};
以上就是php5.5 ~ php7.2 新特性整理 的详细内容。