PHP 5 introduces Type Hinting. But it does so in the usual PHP way: It’s neither fish nor fowl. You’ll be exited at first when you discover the new feature. And then you’ll be disappointed. Because things do not work like you’d expect. Suppose you do something like this:
<?phpfunction foo ( string $bar) {echo $bar;- }
foo ('Hello World!');?>
What happens, when you execute the above code? You will get a rather cryptic error message:
Catchable fatal error: Argument 1 passed to foo() must be an instance of string, string given.
So the method expected a string as the first parameter and you passed a string to it, but PHP chose to throw up regardless. Great, isn’t it? Of course, the reason for this is right at the very bottom of the PHP documentation on Type Hinting. All the way down the page below a number of examples:
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.
Doh!
