From: Rasmus Lerdorf [mailto:ras...@lerdorf.com] 

> How is that hard?
>
>   function age_lookup($age) {
>     return $coll->find(array('age' => (int)$age);
>   }
>
> but again here, doing a strong type check on the parameter isn't making your 
> life easier. It simply pushes the responsibility to the caller and introduces 
> a tricky unrecoverable error that will drive you crazy unless you have 100% 
> regression test coverage (which is kind of impossible since the number of 
> inputs is infinite) or great static analysis tools.
PHP is not a compiled language, so you end up not catching these until runtime 
which is obviously sub-optimal.

> -Rasmus

This will silently fail in a very bad way when the caller accidentally passes 
in (for example) an array. With a scalar type hint it would die loudly (quickly 
alerting the developer to the problem) and code analysis tools (even just a 
decent IDE) could highlight the error even before it is executed.

The point here is that the caller is already responsible to make sure it passes 
the right value, but the language currently offers no means of documenting 
that, and no easy way to ensure it.

There's obviously a question about how much juggling to allow with a scalar 
type hint. IMO the obvious stuff ("36" to 36, etc.) is perfect, but passing 
"foobar" or array(1,2,3) to an int should throw an error.

John Crenshaw
Priacta, Inc.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to