传递给 Y 的参数 X 必须是布尔值的实例,布尔值给定 - PHP7

Argument X passed to Y must be an instance of boolean, boolean given - PHP7

给定代码

<?php
function a(boolean $value){
    var_dump($value);
}
a(true);

我收到错误

TypeError: Argument 1 passed to a() must be an instance of boolean, boolean given

这是怎么回事?

boolean 的唯一有效类型提示是 bool。根据 documentation boolean 在类型提示中不被识别为 bool 的别名。相反,它被视为 class 名称。 int(标量)和 integer(class 名称)也是如此,这将导致错误

TypeError: Argument 1 passed to a() must be an instance of integer, integer given

在这种特定情况下,需要 class boolean 的对象,但传递了 true(bool, scalar)。

有效代码是

<?php
function a(bool $value){
    var_dump($value);
}
a(true);

哪个结果是

bool(true)