Re: Re: Insult my code!
| From: | David Otton | Date: | Tue, 13 Oct 2009 07:38:33 +0000 |
| Subject: | Re: Re: Insult my code! | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-298878@lists.php.net to get a copy of this message | ||
2009/10/13 Eric Bauman <baumane@livejournal.dk>:
> *sigh* sometimes I really wish PHP allowed one to be a bit more heavy-handed
> with types (optional real type hinting would be nice).
There's a scalar type-hinting patch floating around somewhere, but
then your code would only work on machines with that patch.
> I guess I only ever worried about string (from DB) and int (internal call)
> as in my specific use I would never be passing a float.
> You make an excellent point however; I suppose in the interests of
> completeness, forward compatibility etc. I should take into account more
> possibilities. Perhaps I should just throw an exception in deposit() etc. if
> the argument isn't int and worry about converting elsewhere.
I can see two choices - either throw an exception in checkInt() if
it's passed anything except an int or a string (which is fine -
because you've made the assumption explicit) or change the checkInt()
so it deals with ints-masquerading-as-floats. Try:
function checkInt($x) {
return((string)((int)$x) == $x);
}
> Also thanks for the sample TestCase code! I've never really thought about
> unit testing in PHP, despite doing so in Java etc. Reading about PHPUnit
> brought me on to phpUnderControl - interesting stuff!
Yup. Pretty graphs. That's not the best unit test example to work
from, BTW - the manual will give you better advice than I.