On Thu, May 27, 2010 at 10:34, Arvids Godjuks <arvids.godj...@gmail.com> wrote:
> Please read more carefully - what I mean that is we deal mostly with
> numbers witch are represented as strings, because all data that comes
> from external sources are STRING regardless of actual contents - be
> that integer or float - no matter. I don't want to make my code look
> like this:
>
> function doSomeStuffWithDbData(int $id, string $name, int $someFlag) {
> }
>
> $sql = 'SELECT id, name, some_flag, .... FROM table WHERE .....';
> $res = mysqli_query($db, $sql);
> $row = $res->fetch_assoc();
> doSomeStuffWithDbData((int)$row['id'], $row['name'], (int)$row['some_flag']);
>
> And so on. There is no meaning in doing explicit type casts and then
> check the type again! Because if you have bogus data like "123abc"
> with type cast you cast it to 123 integer and you function check for
> integer will pass without any notice!
>
> Hm, it just strike me. If you want a strict type checks, you have to
> convert your data before you pass it when it comes from outside (and
> it always does by the way! - databases, request data, files). So
> basically you will first convert it and then pass to functions to
> avoid inconsistent type errors. And UPS! Magically you have no errors!
> because $data = '123abc'; $data = (int)$data; results in pure 123 int.
> No benefit in using type hints at all.

If you don't know whether the user/database provided information you
have is correct before you pass it along to something else, I would
say that the code indeed is bad. Unless you regard "123abc" as a valid
value from your user, don't allow the user to give you that value. If
you regard it as a valid value, then it's not an int thus you wouldn't
want to type cast it in the first place.

-- 
Daniel Egeberg

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to