Re: Return Type Hinting for Methods RFC
| From: | Rasmus Lerdorf | Date: | Thu, 22 Dec 2011 18:41:28 +0000 |
| Subject: | Re: Return Type Hinting for Methods RFC | ||
| References: | 1 2 3 4 5 6 7 8 9 10 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-57009@lists.php.net to get a copy of this message | ||
On 12/22/2011 07:08 AM, Keloran wrote:
> i would love to see this expanded aswell (the way type hinting on function
> variables was supposed to be), so that it could be
>
> string, int
>
> e.g.
> function int test(bool $tester) {
> if ($tester) { return 5; }
> return 99;
> }
Return type hinting needs to be aligned with parameter type hinting, and
as has been pointed out many times on this list, type hinting for
interchangable scalar types is a really bad idea. It will push all type
checking up to the caller of the underlying functions/methods. PHP is
primarily a Web scripting language and the Web isn't typed. Having stuff
like this break:
if(age_check($_POST['age'])) { do_stuff(); }
because the author of the age_check() function added an int type hint
just doesn't make any sense. It would cause everyone to have to start
casting things everywhere, just in case. eg.
if(age_check((int)$_POST['age'])) { do_stuff(); }
This is not a step forward. If the author of age_check() really doesn't
want to accept type-juggled arguments, then it is easy enough to do a
strict type check in the function itself. This puts the effort in the
correct place and doesn't encourage this type of coding.
-Rasmus