Heh... Please do a DB select and make a var_dump on the rows from
database. You will see things like:

array(3) {
  ["id"]=>
  string(1) "1"
  ["ref_id"]=>
  string(2) "15"
  ["name"]=>
  string(7) "Bla bla"
}

string, string and string again.

And that breaks the concept of strict type hinting. Because we get
data from DB in STRING type, regardless of the type of the DB fields.
And that's my argument for auto-converting type hints when it is
possible without data loss. I expect PHP to pass indexes "id" and
"ref_id" to function, witch expects int without any notices or errors
- just do the bloody thing with converting to int and do your work. If
I pass "name" index to that function - it should fail with a Fatal
error.

So, stop twisting my words and taking sentences out of the context,
please! I don't want to argue on things that I described in detail and
people just skim them trough just to reply with critics just for the
sake of making a reply.

2010/5/27 Daniel Egeberg <daniel.egeb...@gmail.com>:
> 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