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

半腔热情 提交于 2019-12-19 14:41:20

问题


Given code

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

I receive error

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

What is going on here?


回答1:


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: Argument 1 passed to a() must be an instance of integer, integer given

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

Valid code is

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

which result is

bool(true)



来源:https://stackoverflow.com/questions/45348751/argument-x-passed-to-y-must-be-an-instance-of-boolean-boolean-given-php7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!