Re: RE: Optional scalar type hinting
| From: | Paweł Stradomski | Date: | Thu, 03 Jan 2008 23:08:43 +0000 |
| Subject: | Re: RE: Optional scalar type hinting | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-34369@lists.php.net to get a copy of this message | ||
W liście Stanislav Malyshev z dnia czwartek 03 stycznia 2008:
> > type-hinting is asserting.
> > checking of types is needed only on interface-border points (where
>
> With strict checking, that means instead of calling:
> foo($bar);
> you'd have now to do:
> if(is_integer($bar)) {
> // stupid foo() call would blow up if passed not integer, so I have
> to manually handle it
> // oh wait, actually I don't have any way to handle it - I need
> foo() to be called to proceed
> // so let's just
> die("can't call foo() because \$bar is not integer");
> }
> foo($bar);
>
This code is just a good argument *FOR* type hints. When foo is:
function foo(int $bar) {...}
and you want the code to die if $bar is not integer, then foo($bar) would be
the way to go, without the need for the mess you posted. And if you know that
you have either an int or string representing an integer in $bar, then
foo((integer)$bar) would work just well.
The only problem with the cast is that (integer)'abc' is 0, but it has been so
for a long time and there is no need to change it. is_numeric check solves
the problem.
--
Paweł Stradomski