Class type, instance of

There is a function:

function something($param = false) {
}

I want to set a specific type of $param to the input of a function. If it’s an Array then this works:

function something(Array $param = array()) {
}

And if it’s a Boolean, then for some reason it’s not:

…must be an instance of boolean, boolean given…


Answer 1, authority 100%

You have now set the arguments to their default values.

If I’m not mistaken, somewhere in the documentation it says that arguments can only be typed with an array or a class. Simple types: string, logic, number and others are not allowed. He apparently perceives the name of the types as the names of classes and therefore swears, but I’m not sure.

To check, you need to write code inside the function that would check the type of the variable using the appropriate functions (is_array, is_string, etc.) and would throw an exception if there is a mismatch.

Confirmation:

Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn’t supported.