2009/10/13 Eric Bauman <baum...@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.

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to