• PHP7.X 版本新特性摘选


    本文摘自 php.net


    一、PHP7.0X

    1.1 标量类型声明

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

    
        // Coercive mode
        function sumOfInts(int ...$ints)
        {
            return array_sum($ints);
        }
    
        var_dump(sumOfInts(2, '3', 4.1));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    以上例程会输出

    int(9)
    
    • 1

    1.2 返回值类型声明

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

    
    
    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]));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    以上例程会输出

    Array
    (
        [0] => 6
        [1] => 15
        [2] => 24
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    1.3 null 合并运算符

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

    
        $username = 'Chon';
        echo $username ?? 'nobody';
    
    • 1
    • 2
    • 3

    以上例程会输出

    Chon
    
    • 1

    1.4 <=> 太空船操作符

    
        // 整数
        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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

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

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

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

    
    	define('ANIMALS', [
        'dog',
        'cat',
        'bird'
    	]);
    
    	echo ANIMALS[1];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    以上例程会输出

    cat
    
    • 1

    1.6 整数除法函数 intdiv()

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

    
        var_dump(intdiv(10, 3));
    
    • 1
    • 2

    以上例程会输出

    int(3)
    
    • 1

    二、PHP7.1.X

    2.1 可为空(Nullable)类型

    参数以及返回值的类型现在可以通过在类型前加上一个问号使之允许为空。 当启用这个特性时,传入的参数或者函数返回的结果要么是给定的类型,要么是 null 。

    
        
        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();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    以上例程会输出

    string(10) "elePHPant"
    NULL
    string(10) "elePHPant"
    NULL
    Uncaught Error: Too few arguments to function test(), 0 passed in...
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.2 类常量可见性

    
    
    class ConstDemo
    {
        const PUBLIC_CONST_A = 1;
        public const PUBLIC_CONST_B = 2;
        protected const PROTECTED_CONST = 3;
        private const PRIVATE_CONST = 4;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.3 Symmetric array destructuring

    短数组语法([])现在作为list()语法的一个备选项,可以用于将数组的值赋给一些变量(包括在foreach中)。

    
        $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
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    2.4 list()现在支持键名

    现在list()和它的新的[]语法支持在它内部去指定键名。这意味着它可以将任意类型的数组 都赋值给一些变量(与短数组语法类似)

    
        $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
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

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

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

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

    
    	var_dump("abcdef"[-2]);
    	var_dump(strpos("aabbcc", "b", -3));
    
        $string = 'bar';
    	echo "The last character of '$string' is '$string[-1]'.\n";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    以上例程会输出

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

    三、 PHP7.2.x

    3.1 允许重写抽象方法(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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    以上例程会输出

    string(10) "elePHPant"
    NULL
    string(10) "elePHPant"
    NULL
    Uncaught Error: Too few arguments to function test(), 0 passed in...
    
    • 1
    • 2
    • 3
    • 4
    • 5

    四、PHP7.4.x

    4.1 属性添加限定类型

    
    
    class User {
        public int $id;
        public string $name;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

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

    4.3 空合并运算符赋值

    
        $array['key'] ??= computeDefault();
        // 等同于以下旧写法
        if (!isset($array['key'])) {
            $array['key'] = computeDefault();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4.4 数组展开操作

    $parts = ['apple', 'pear'];
    $fruits = ['banana', 'orange', ...$parts, 'watermelon'];
    // ['banana', 'orange', 'apple', 'pear', 'watermelon'];
    
    • 1
    • 2
    • 3
  • 相关阅读:
    图片如何制作gif动画?1分钟教会你快速制作gif
    如何使用“–format”和输出模板成为 Docker CLI 高级用户
    【Redis学习笔记】第二章【2.4】Redis数据类型--set
    2022年上半年中国数字藏品(NFT)市场分析总结
    第二篇 渲染框架2.x
    C++ Reference: Standard C++ Library reference: C Library: cwctype: iswcntrl
    今天就分享一段话
    2024年2月19日-2月25日(全面进行+收集免费虚幻商城资源,20小时,合计2561小时,剩余7439小时)
    剑指 Offer 06. 从尾到头打印链表
    springboot全局异常处理详解
  • 原文地址:https://blog.csdn.net/qq_35453862/article/details/126542735