Why type hint when you can type cast

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Apart from the fatal error generated by type hinting when not passed values as array or object as described on the php manual, is there any thing else that will make me use type hinting over type casting?


Re: Why type hint when you can type cast

On 7/31/13 7:12 AM, Bruce Lee wrote:
Quoted text here. Click to load it

The major purpose of type hinting IS to get the fatal error to detect
that you passed the wrong type to the function. If your interface says
you need to pass a foo, you want to make sure you pass a foo. The type
hint says you don't need (as much) to validate that the correct type was
passed (and you rarely have a good recovery path if the wrong type was

If you don't need to require a given type to be sent to you, but have
reasonable recovery paths for other types, don't use a type hint to
require it.

Site Timeline