PHP7.X 版本新特性摘选

本文摘自 php.net


一、PHP7.0X

1.1 标量类型声明

标量类型声明 有两种模式: 强制 (默认) 和 严格模式。 现在可以使用下列类型参数(无论用强制模式还是严格模式): 字符串(string), 整数 (int), 浮点数 (float), 以及布尔值 (bool)。它们扩充了PHP5中引入的其他类型:类名,接口,数组和 回调类型。

<?php
    // Coercive mode
    function sumOfInts(int ...$ints)
    {
    
    
        return array_sum($ints);
    }

    var_dump(sumOfInts(2, '3', 4.1));

以上例程会输出

int(9)

1.2 返回值类型声明

PHP 7 增加了对返回类型声明的支持。 类似于参数类型声明,返回类型声明指明了函数返回值的类型。可用的类型与参数声明中可用的类型相同。

<?php

function arraysSum(array ...$arrays): array
{
    
    
    return array_map(function(array $array): int {
    
    
        return array_sum($array);
    }, $arrays);
}

print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));

以上例程会输出

Array
(
    [0] => 6
    [1] => 15
    [2] => 24
)

1.3 null 合并运算符

由于日常使用中存在大量同时使用三元表达式和 isset()的情况, 我们添加了 null 合并运算符 (??) 这个语法糖。如果变量存在且值不为**null**, 它就会返回自身的值,否则返回它的第二个操作数。

<?php
    $username = 'Chon';
    echo $username ?? 'nobody';

以上例程会输出

Chon

1.4 <=> 太空船操作符

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

1.5 通过 define() 定义常量数组

Array 类型的常量现在可以通过 define() 来定义。在 PHP5.6 中仅能通过 const 定义。

use 运算符 被进行了扩展以支持在类中导入外部的函数和常量。 对应的结构为 use functionuse const

<?php
	define('ANIMALS', [
    'dog',
    'cat',
    'bird'
	]);

	echo ANIMALS[1];

以上例程会输出

cat

1.6 整数除法函数 intdiv()

新加的函数 intdiv() 用来进行 整数的除法运算。

<?php
    var_dump(intdiv(10, 3));

以上例程会输出

int(3)

二、PHP7.1.X

2.1 可为空(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...

2.2 类常量可见性

<?php

class ConstDemo
{
    
    
    const PUBLIC_CONST_A = 1;
    public const PUBLIC_CONST_B = 2;
    protected const PROTECTED_CONST = 3;
    private const PRIVATE_CONST = 4;
}

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

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

2.5 支持为负数的字符串偏移量

现在所有支持偏移量的字符串操作函数 都支持接受负数作为偏移量,包括通过[]{}操作字符串下标。在这种情况下,一个负数的偏移量会被理解为一个从字符串结尾开始的偏移量。

use 运算符 被进行了扩展以支持在类中导入外部的函数和常量。 对应的结构为 use functionuse const

<?php
	var_dump("abcdef"[-2]);
	var_dump(strpos("aabbcc", "b", -3));

    $string = 'bar';
	echo "The last character of '$string' is '$string[-1]'.\n";

以上例程会输出

string (1) "e"
int(3)
The last character of 'bar' is 'r'.

三、 PHP7.2.x

3.1 允许重写抽象方法(Abstract method)

当一个抽象类继承于另外一个抽象类的时候,继承后的抽象类可以重写被继承的抽象类的抽象方法。

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

以上例程会输出

string(10) "elePHPant"
NULL
string(10) "elePHPant"
NULL
Uncaught Error: Too few arguments to function test(), 0 passed in...

四、PHP7.4.x

4.1 属性添加限定类型

<?php

class User {
    
    
    public int $id;
    public string $name;
}

4.2 箭头函数 提供了一种更简洁的定义函数的方法。

<?php
    $factor = 10;
    $nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]);
    // $nums = array(10, 20, 30, 40);

4.3 空合并运算符赋值

<?php
    $array['key'] ??= computeDefault();
    // 等同于以下旧写法
    if (!isset($array['key'])) {
    
    
        $array['key'] = computeDefault();
    }

4.4 数组展开操作

$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];

猜你喜欢

转载自blog.csdn.net/qq_35453862/article/details/126542735