Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 956
mleko Avatar asked Jul 27 '17 10:07

mleko


Video Answer


1 Answers

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)

like image 95
mleko Avatar answered Oct 11 '22 13:10

mleko