Search code examples
phptypestypeerrormixed

PHP TypeError: Parameter must be an instance of lib\util\mixed, string given


In my PHP file I have created a function with a parameter which can be of any type. Because of this, I declared this parameter as mixed. It seems the mixed type is linked to the folder location where the class is headed; lib\util. The error says instance of lib\util\mixed.

protected function curlSetOpt(int $param1, mixed $param2): bool
{
    return anotherFunction($this->variable, $param1, $param2);
}

The moment I call this method using a string, a TypeError occurs.

Argument 2 passed to lib\util\Class::function() must be an instance of lib\util\mixed, string given

Renaming the type from mixed to string works, but also other types are welcome.


Solution

  • mixed is not a valid type for hinting in PHP. The valid types are listed here: http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

    The slightly cryptic error message is because PHP is assuming you are trying to restrict the type to a class within your namespace: lib\util\mixed.

    If you don't want to restrict the type, just don't add a type hint.