How to resolve “must be an instance of string, string given” prior to PHP 7?

Here is my code:

function phpwtf(string $s) {
    echo "$s\n";
}
phpwtf("Type hinting is da bomb");

Which results in this error:

Catchable fatal error: Argument 1 passed to phpwtf() must be an instance of string, string given

It’s more than a little Orwellian to see PHP recognize and reject the desired type in the same breath. There are five lights, damn it.

What is the equivalent of type hinting for strings in PHP? Bonus consideration to the answer that explains exactly what is going on here.

9 Answers
9

Leave a Comment