php8 版本的一些新特性

人家说你看着小,其实并不是你娃娃脸,而是你穿得土。

 

php8 的一些新增特性

1、新增对联合类型的支持

  联合类型允许一个变量拥有多个类型的值,而不是一个(参考 C 语言的联合类型很好理解)。

class Number {
    private int|float $number;
 
    public function setNumber(int|float $number): void {
        $this->number = $number;
    }
 
    public function getNumber(): int|float {
        return $this->number;
    }
}

$number = new Number();

$number->setNumber(5);

var_dump($number->getNumber());

$number->setNumber(11.54);

var_dump($number->getNumber());

 

2、新增 ValueError 异常

  PHP 8 引入了新的名为 ValueError 的内置异常类,它继承自 Exception 基类。每次当你传递值到函数时,如果是一个无效类型,则会抛出该异常,在 PHP 8 之前,这样的操作会导致警告。

declare(strict_types=1);

/**
 * 传递数组到 array_rand,类型正确,但是 array_rand 期望传入的是非空数组
 * 所以会抛出 ValueError 异常
 */
array_rand([], 0);

/**
 * json_decode 的深度参数必须是有效的正整型值,
 * 所以这里也会抛出 ValueError 异常
 */
json_decode('{}', true, -1);

 

3、重写方法时允许可变参数

  当我们在子类重写父类方法时,任何数量的参数现在都可以被替换成可变参数,只要对应参数类型是兼容的即可

declare(strict_types=1);

class A {
    public function method(int $many, string $parameters, $here) {

    }
}
class B extends A {
    public function method(...$everything) {
        var_dump($everything);
    }
}

$b = new B();
$b->method('i can be overwritten!');

 

4、静态返回类型

  PHP 8 中可以使用 static 关键字标识某个方法返回该方法当前所属的类,即使它是继承的(后期静态绑定)

declare(strict_types=1);

class Test {
    public function doWhatever(): static {
        // Do whatever.
        return $this;
    }
}

 

5、对象的类名字面量

  PHP 8 中可以使用 $object::class 获取对象的类名,其返回结果和 get_class($object) 一样

declare(strict_types=1);

class Test {
}

$test = new Test();
var_dump($test::class);
var_dump(get_class($test));

 

6、变量语法调整

  new 和 instanceof 关键字现在可以被用于任意表达式:

declare(strict_types=1);

class Foo {}
class Bar {}

$names = ['Foo', 'Bar'];
$class = new ($names[array_rand($names)]);

var_dump($class);

 

7、Stringable 接口

  PHP 8 引入了新的 Stringable 接口,只要某个类实现了 __toString 方法,即被视作自动实现了 Stringable 接口(咋和 Go 接口实现有点像),而不必显式声明实现该接口。

declare(strict_types=1);

class Foo {
    public function __toString() {
        return 'I am a class';
    }
}

$obj = new Foo;
var_dump($obj instanceof Stringable);

 

8、Trait 现在可以定义抽象私有方法

declare(strict_types=1);

trait MyTrait {
    abstract private function neededByTheTrait(): string;

    public function doSomething() {
        return strlen($this->neededByTheTrait());
    }
}

class TraitUser {
    use MyTrait;

    // 支持该语法
    private function neededByTheTrait(): string { }

    // 不支持该语法 (错误的返回类型)
    // private function neededByTheTrait(): stdClass { }

    // 支持该语法 (非静态方法变成了静态方法)
    // private static function neededByTheTrait(): string { }
}

 

9、throw 现在可以被用作表达式

  throw 语句现在可以用在只允许表达式出现的地方,例如箭头函数、合并运算符和三元运算符等

declare(strict_types=1);

$callable = fn() => throw new Exception();

$nullableValue = null;

// $value 是非空的
$value = $nullableValue ?? throw new \InvalidArgumentException();

 

10、参数列表中允许出现可选的尾部逗号

  和数组中的尾部逗号类似,现在也可以在参数列表中定义一个尾部逗号

function method_with_many_arguments($a, $b, $c, $d,) {
    var_dump("this is valid syntax");
}

method_with_many_arguments(1,2,3,4,);

 

11、捕获异常而不存储到变量

  现在可以编写 catch (Exception) 代码来捕获异常而不必将其存储到一个变量中

declare(strict_types=1);

$nullableValue = null;

try {
    $value = $nullableValue ?? throw new \InvalidArgumentException();
} catch (\InvalidArgumentException) {
    var_dump("Something went wrong");
}

 

12、新增对 mixed 类型的支持

  PHP 8 引入了新的名为 mixed 的类型,该类型等价于 array|bool|callable|int|float|null|object|resource|string

declare(strict_types=1);

function debug_function(mixed ...$data)
{
    var_dump($data);
}

debug_function(1, 'string', []);

 

13、新增对注解的支持

  注解绝对是 PHP 8 引入的最大新特性之一,一开始理解起来可能有点困难(不过有 Java 基础的话会很简单)。简而言之,注解允许你添加元数据到 PHP 函数、参数、类等,这些元数据随后可以通过可编程方式获取,在 PHP 7 或者更低版本中实现类似功能需要解析代码注释块,而通过注解可以直接访问深度集成到 PHP 自身的这些信息。

https://wiki.php.net/rfc/attributes_v2
https://wiki.php.net/rfc/attribute_amendments
https://wiki.php.net/rfc/shorter_attribute_syntax
https://wiki.php.net/rfc/shorter_attribute_syntax_change

 

 

14、新增构造函数属性提示支持

  这个新特性只是一个语法简写而言,可以将属性声明和构造函数属性初始化合并到一起

declare(strict_types=1);

class User {
    public function __construct(
        public int $id,
        public string $name,
    ) {}
}

$user = new User(1, 'Marcel');

var_dump($user->id);
var_dump($user->name);

 

15、新增 match 表达式支持

  match 表达式和 switch 分支语句类型,但是语义上更加安全并且可以直接返回值

declare(strict_types=1);

echo match (1) {
    0 => 'Foo',
    1 => 'Bar',
    2 => 'Baz',
};

 

16、新增对空安全运算符 ?-> 的支持

  当该运算符的左侧评估为 null 时,整个代码链路的执行将会被终止并整体评估为 null。如果不为 null 的话,则和普通的 -> 运算符功能一致

declare(strict_types=1);

class User {
    public function getAddress() {}
}

$user = new User();

$country = $user?->getAddress()?->country?->iso_code;

var_dump($country);

 

17、新增对命名参数的支持

  命名参数允许基于参数名称传递参数到函数,而不是参数所在的位置,这样一来,函数参数就可以自解释并且与顺序无关,并且允许跳过默认值

declare(strict_types=1);

array_fill(start_index: 0, num: 100, value: 50);

 

posted @ 2022-02-12 08:54  方达达  阅读(158)  评论(0编辑  收藏  举报