php7.1 新增功能
1.可为空(nullable)类型参数和返回值的类型声明可以通过在类型名称前添加一个问号(?)来标记为空(null)。表明函数参数或者返回值的类型要么为指定类型,要么为 null。
看下例子:
function testreturn(?string $name){
return $name;
}
var_dump(testreturn('yangyi'));
var_dump(testreturn(null));
var_dump(testreturn2());
打印输出:
$ php php71.phpstring(6) "yangyi"null
php fatal error: uncaught argumentcounterror:
too few arguments to function testreturn(),
0 passed in php71.php on line 22 and exactly 1
expected in php71.php:14stack trace:#0 php71.php(22):
testreturn()#1 {main}
thrown in php71.php on line 14
如上如:第三个报了一个致命的错误。
再来看下,函数返回值是nullable的情况:
function testreturn3() : ?string{
//return "abc";
//return null;}
var_dump(testreturn3());
如果加了? 要么返回 string ,要么返回null。不能啥也不返还。会报错。
2.void返回类型php7.0 添加了指定函数返回类型的特性,但是返回类型却不能指定为 void,7.1 的这个特性算是一个补充。定义返回类型为 void 的函数不能有返回值,即使返回 null 也不行:
function testreturn4() : void{
//1. 要么啥都不返还 ok
//2. 要么只return; ok
//return;
//3. return null 也会报错
//return null;
//4. return 4 会报错
//return 4;}
fatal error: a void function must not return a value in /php71.php on line 70
还有就是,void 只能用于返回值,不能用于参数中。比如下面的会报错:
function testreturn6(void $a) : void{}
var_dump(testreturn6());
php fatal error: void cannot be used as a parameter type in php71.php on line 73
如果在类的继承中,申明为void返回类型的方法,子类要是继承重写,也只能返回void, 否则会触发错误:
<?php class foo{
public function bar(): void {
}
}class foobar extends foo{
// 覆盖失败
public function bar(): array {
// fatal error: declaration of foobar::bar() must be compatible with foo::bar(): void
}
}
所以,你必须这样,就不会报错:
class foo{
public $a; public function bar(): void {
$this->a = 2;
}
}class foobar extends foo{
// 覆盖成功
public function bar(): void {
$this->a = 3;
}
}
3.list 的方括号([])简写以及增加指定key可以用list 来快速遍历得到数组里面的值。现在可以用[]简写了。
$data = [
[1, 'tom'],
[2, 'fred'],
];// list() stylelist($id1, $name1) = $data[0];// [] style[$id1, $name1] = $data[0];// list() styleforeach ($data as list($id, $name)) { // logic here with $id and $name}// [] styleforeach ($data as [$id, $name]) { // logic here with $id and $name}
此外,此次更新的list,针对索引数组,还可以指定 key,这个升级非常棒,非常方便。
$data = [
["id" => 1, "name" => 'tom'],
["id" => 2, "name" => 'fred'],
];// list() stylelist("id" => $id1, "name" => $name1) = $data[0];
// [] style["id" => $id1, "name" => $name1] = $data[0];
// list() styleforeach ($data as list("id" => $id, "name" => $name)) {
// logic here with $id and $name
}
// [] styleforeach ($data as ["id" => $id, "name" => $name]) {
// logic here with $id and $name}
在这个功能没有之前,我们一般会用while + each 的方式来用list 遍历索引数组:
$data = [
["id" => 1, "name" => 'tom'],
["id" => 2, "name" => 'fred'],
];while (list($id, name) = each($data)) {
echo "$key => $val\n";
}
注意:php 7.2 中已经将 each 函数移除了!所以,就不要用这种方式来遍历索引数组了
3.类常量可见范围设定之前类里面额常量用const申明,是没有可见属性的。现在把方法的可见属性移植了过来:
<?php
class constdemo
{ // 常量默认为 public
const public_const = 0; // 可以自定义常量的可见范围
public const public_const_b = 2; protected const protected_const = 3;
private const private_const = 4; // 多个常量同时声明只能有一个属性
private const foo = 1, bar = 2;
}
使用方法和类的方法一样。就不多详述了。
4.支持负的字符串偏移有2个更新,1是字符串直接取,2是strpos函数第三个参数支持负数。表示从尾部取。
var_dump("abcdef"[-2]); // evar_dump(strpos("aabbcc", "b", -3)); //3
string变量可以直接取值,不用通过变量名,是在php5.5加入的。现在可以从尾部取:
var_dump("abcdef"[-2]); // 从末尾取倒数第2个字符:evar_dump("abcdef"[2]);
// 从前面取第2个,从0开始:c$string = 'bar';echo $string[1], $string[-1]; // a r
5.多条件 catch在以往的 try … catch 语句中,每个 catch 只能设定一个条件判断:
try { // some code...} catch (exceptiontype1 $e) {
// 处理 exceptiontype1} catch (exceptiontype2 $e) {
// 处理 exceptiontype2} catch (exception $e) { // ...}
现在可以多个一起处理。用”|” 分割。
try {
// some code...
} catch (exceptiontype1 | exceptiontype2 $e) {
// 对于 exceptiontype1 和 exceptiontype2 的处理
} catch (exception $e) {
// ...}
php7.2php 7.2大都是底层的更新,提高性能。没有太大常用语法层面的更新,这里就略过了。
之前写过php7.0以及老版本的php各大版本的跟新点以及新功能。今天看下php7.1和php7.2的新功能。
php7.1 新增功能1.可为空(nullable)类型参数和返回值的类型声明可以通过在类型名称前添加一个问号(?)来标记为空(null)。表明函数参数或者返回值的类型要么为指定类型,要么为 null。
看下例子:
function testreturn(?string $name){
return $name;
}
var_dump(testreturn('yangyi'));
var_dump(testreturn(null));
var_dump(testreturn2());
打印输出:
$ php php71.phpstring(6) "yangyi"null
php fatal error: uncaught argumentcounterror:
too few arguments to function testreturn(), 0 passed in php71.php on line 22 and exactly
1 expected in php71.php:14stack trace:#0 php71.php(22): testreturn()#1 {main}
thrown in php71.php on line 14
如上如:第三个报了一个致命的错误。
再来看下,函数返回值是nullable的情况:
function testreturn3() : ?string{
//return "abc";
//return null;}
var_dump(testreturn3());
如果加了? 要么返回 string ,要么返回null。不能啥也不返还。会报错。
2.void返回类型php7.0 添加了指定函数返回类型的特性,但是返回类型却不能指定为 void,7.1 的这个特性算是一个补充。定义返回类型为 void 的函数不能有返回值,即使返回 null 也不行:
function testreturn4() : void{
//1. 要么啥都不返还 ok
//2. 要么只return; ok
//return;
//3. return null 也会报错
//return null;
//4. return 4 会报错
//return 4;}
fatal error: a void function must not return a value in /php71.php on line 70
还有就是,void 只能用于返回值,不能用于参数中。比如下面的会报错:
function testreturn6(void $a) : void{}
var_dump(testreturn6());
php fatal error: void cannot be used as a parameter type in php71.php on line 73
如果在类的继承中,申明为void返回类型的方法,子类要是继承重写,也只能返回void, 否则会触发错误:
<?php class foo{
public function bar(): void {
}
}class foobar extends foo{
// 覆盖失败
public function bar(): array {
// fatal error: declaration of foobar::bar() must be compatible with foo::bar(): void
}
}
所以,你必须这样,就不会报错:
class foo{
public $a; public function bar(): void {
$this->a = 2;
}
}class foobar extends foo{
// 覆盖成功
public function bar(): void {
$this->a = 3;
}
}
3.list 的方括号([])简写以及增加指定key可以用list 来快速遍历得到数组里面的值。现在可以用[]简写了。
$data = [
[1, 'tom'],
[2, 'fred'],
];// list() stylelist($id1, $name1) = $data[0];// [] style[$id1, $name1] = $data[0];
// list() styleforeach ($data as list($id, $name)) { // logic here with $id and $name}
// [] styleforeach ($data as [$id, $name]) { // logic here with $id and $name}
此外,此次更新的list,针对索引数组,还可以指定 key,这个升级非常棒,非常方便。
$data = [
["id" => 1, "name" => 'tom'],
["id" => 2, "name" => 'fred'],
];// list() stylelist("id" => $id1, "name" => $name1) = $data[0];
// [] style["id" => $id1, "name" => $name1] = $data[0];
// list() styleforeach ($data as list("id" => $id, "name" => $name)) {
// logic here with $id and $name}// [] styleforeach ($data as ["id" => $id, "name" => $name]) {
// logic here with $id and $name}
在这个功能没有之前,我们一般会用while + each 的方式来用list 遍历索引数组:
$data = [
["id" => 1, "name" => 'tom'],
["id" => 2, "name" => 'fred'],
];while (list($id, name) = each($data)) { echo "$key => $val\n";
}
注意:php 7.2 中已经将 each 函数移除了!所以,就不要用这种方式来遍历索引数组了
3.类常量可见范围设定之前类里面额常量用const申明,是没有可见属性的。现在把方法的可见属性移植了过来:
<?php
class constdemo
{ // 常量默认为 public
const public_const = 0; // 可以自定义常量的可见范围
public const public_const_b = 2; protected const protected_const = 3; private const private_const = 4; // 多个常量同时声明只能有一个属性
private const foo = 1, bar = 2;
}
使用方法和类的方法一样。就不多详述了。
4.支持负的字符串偏移有2个更新,1是字符串直接取,2是strpos函数第三个参数支持负数。表示从尾部取。
var_dump("abcdef"[-2]); // evar_dump(strpos("aabbcc", "b", -3)); //3
string变量可以直接取值,不用通过变量名,是在php5.5加入的。现在可以从尾部取:
var_dump("abcdef"[-2]); // 从末尾取倒数第2个字符:evar_dump("abcdef"[2]);
// 从前面取第2个,从0开始:c$string = 'bar';echo $string[1], $string[-1];
// a r
5.多条件 catch在以往的 try … catch 语句中,每个 catch 只能设定一个条件判断:
try { // some code...} catch (exceptiontype1 $e) {
// 处理 exceptiontype1} catch (exceptiontype2 $e) {
// 处理 exceptiontype2} catch (exception $e) {
// ...}
现在可以多个一起处理。用”|” 分割。
try {
// some code...
} catch (exceptiontype1 | exceptiontype2 $e) {
// 对于 exceptiontype1 和 exceptiontype2 的处理
} catch (exception $e) {
// ...}
php7.2php 7.2大都是底层的更新,提高性能。没有太大常用语法层面的更新,这里就略过了。
相关推荐:
谈谈 php7新增功能
php7新增 功能,php7新增功能
php5.2至5.6的新增功能详解
以上就是实例详解php7.1和7.2新增功能的详细内容。