Re: Return Type Hinting for Methods RFC

From: Date: Thu, 22 Dec 2011 19:56:44 +0000
Subject: Re: Return Type Hinting for Methods RFC
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  Groups: php.internals 
Request: Send a blank email to internals+get-57028@lists.php.net to get a copy of this message
On 12/22/2011 11:49 AM, John Crenshaw wrote: > From: Rasmus Lerdorf [mailto:rasmus@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

« previous php.internals (#57028) next »