php7 增加的新特性和变动

作者: dreamfly 分类: php 发布时间: 2020-07-03 14:04

增加了类型声明,可以声明函数参数类型,和函数返回值类型

<?php
declare(strict_types=1);
function add(int $a, int $b): int {
    return $a+$b;
}

echo add(1, 2);
echo add(1.5, 2.6);//报错,参数类型不对

太空船运算符(组合比较符)


<?php
// 整型比较
print( 1 <=> 1);print(PHP_EOL);
print( 1 <=> 2);print(PHP_EOL);
print( 2 <=> 1);print(PHP_EOL);
print(PHP_EOL); // PHP_EOL 为换行符

// 浮点型比较
print( 1.5 <=> 1.5);print(PHP_EOL);
print( 1.5 <=> 2.5);print(PHP_EOL);
print( 2.5 <=> 1.5);print(PHP_EOL);
print(PHP_EOL);

// 字符串比较
print( "a" <=> "a");print(PHP_EOL);
print( "a" <=> "b");print(PHP_EOL);
print( "b" <=> "a");print(PHP_EOL);
?>

结果:

0
-1
1

0
-1
1

0
-1
1

??判空运算符

<?php
//原写法
$username = isset($_GET['user']) ? $_GET['user'] : '';

//现在
$username = $_GET['user'] ?? '';

define() 定义常量数组

<?php

define('ARR',['a','b']);
echo ARR[1];// a

增加AST: Abstract Syntax Tree, 抽象语法树,利于代码重构解耦

PHP5 : PHP代码 -> Parser语法解析 -> OPCODE -> 执行
PHP7 : PHP代码 -> Parser语法解析 -> AST -> OPCODE -> 执行

命名空间引用优化

<?php

// PHP5
use FooLibrary\Bar\Baz\ClassA; 
use FooLibrary\Bar\Baz\ClassB; 
// PHP7
use FooLibrary\Bar\Baz\{ ClassA, ClassB};

匿名函数的引入

<?php
$message = 'hello';

// 没有 "use"
$example = function () {
    var_dump($message);
};
echo $example();

// 继承 $message
$example = function () use ($message) {
    var_dump($message);
};
echo $example();

// Inherited variable's value is from when the function
// is defined, not when called
$message = 'world';
echo $example();

// Reset message
$message = 'hello';

// Inherit by-reference
$example = function () use (&$message) {
    var_dump($message);
};
echo $example();

// The changed value in the parent scope
// is reflected inside the function call
$message = 'world';
echo $example();

// Closures can also accept regular arguments
$example = function ($arg) use ($message) {
    var_dump($arg . ' ' . $message);
};
$example("hello");

$course = '数学';
 $func = function ($value,$key)use ($course){
     echo $value.$course.$key;
 };
 $arr = [
     1=>1,
     2=>2,
     3=>3
 ];
 array_walk($arr,$func);

结果:

Notice: Undefined variable: message in /example.php on line 6
NULL
string(5) "hello"
string(5) "hello"
string(5) "hello"
string(5) "world"
string(11) "hello world"
输出"1数学12数学23数学3"

预加载

预加载功能是指在服务启动时,未运行任何应用程序代码之前,将一组PHP文件加载到内存中,甚至可以对框架进行预加载,以提高性能。如果对预加载代码进行修改,需要重启服务。

[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.enable_cli=1
opcache.preload=preload.php

类属性的类型支持

<?php

class User
{
    private int $id;

    public string $name;
}

NULL合并赋值运算符

<?php

$arr['a'] ??= 'a';
/*等同于*/ $arr['a'] = $arr['a'] ?? 'a'; 

$b ??= 'b'; 
/*等同于*/ $b = $b ?? 'b';

简化匿名函数

$adder = fn($x, $y) => $x + $y;

// 等同于

$adder = function ($x, $y) {
    return $x + $y;
};

$y = 1;
$fn1 = function ($x) use ($y) {
    return $x + $y;
}; 

// 等同于

$fn2 = fn($x) => $x + $y;

// 新的写法省去了 use, 变得更加简洁

新增mb_str_split函数

可为空(Nullable)类型

<?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();

Void 函数

<?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);

短数组语法([])现在作为list()语法的一个备选项

<?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
}

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
var_dump("abcdef"[-2]);
var_dump(strpos("aabbcc", "b", -3));

多异常捕获处理

<?php
try {
    // some code
} catch (FirstException | SecondException $e) {
    // handle first and second exceptions
}

http2部分支持

允许重写抽象方法

<?php

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;
}

使用Argon2算法生成密码散列

新增 ext/PDO(PDO扩展) 字符串扩展类型,为 ext/PDO新增额外的模拟调试信息

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!