传递给Y的参数X必须是boolean的实例,给定的是boolean-PHP7 [英] Argument X passed to Y must be an instance of boolean, boolean given - PHP7

查看:87
本文介绍了传递给Y的参数X必须是boolean的实例,给定的是boolean-PHP7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供代码

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

我收到错误消息

TypeError:传递给a()的参数1必须是boolean(给定为boolean)的实例

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

这是怎么回事?

推荐答案

boolean的唯一有效的typehint是bool.根据文档 boolean不是在typehints中被识别为bool的别名.而是将其视为类名. int(标量)和integer(类名称)相同,这将导致错误

Only valid typehint for boolean is bool. As per documentation boolean isn't recognized as alias of bool in typehints. Instead it is treated as class name. Same goes for int(scalar) and integer(class name), which will result in error

TypeError:传递给a()的参数1必须是整数的实例,给定的整数

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

在这种情况下,应该使用类boolean的对象,但是会传递true(布尔,标量).

In this specific case object of class boolean is expected but true(bool, scalar) is passed.

有效代码为

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

结果是

bool(true)

bool(true)

这篇关于传递给Y的参数X必须是boolean的实例,给定的是boolean-PHP7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆