On 12/22/2011 11:49 AM, John Crenshaw wrote:
> 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.

That's an argument for "scalar" as the type hint, not for "int" vs.
"string". I'm not against "scalar" as a type hint. I am against hints
that prevent interchangeable types from being passed. scalar, array,
object, callable are fine.

-Rasmus

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

Reply via email to