Re: Type hinting - Request for Discussion
| From: | Alban | Date: | Fri, 10 Jul 2009 15:50:04 +0000 |
| Subject: | Re: Type hinting - Request for Discussion | ||
| References: | 1 2 3 4 5 6 7 8 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-44924@lists.php.net to get a copy of this message | ||
Le Fri, 10 Jul 2009 16:16:51 +0100, Alain Williams a écrit :
> On Fri, Jul 10, 2009 at 10:57:22AM -0400, Alban wrote:
>> Le Fri, 10 Jul 2009 14:23:24 +0100, Alain Williams a écrit :
>>
>> > On Fri, Jul 10, 2009 at 08:45:55AM -0400, Alban wrote:
>> >> Hello all,
>> >>
>> >> I don't understand why you focus on " Strict / Weak " question.
>> >
>> > It is really about:
>> >
>> > * type checking and enforcement
>> > or
>> > * type casting and generation of an error if the value cannot be
>> > converted cleanly
>>
>> Thank for your response !
>>
>> I vote for throw an exception if data type won't match. Have one error
>> when you make a mistake is better than have a silently casting which
>> can transform your data in another form than what you want.
>
> I think that we need both.
> * Strict enforcement is useful.
>
> * Doing silent type casting can be useful, a naive example is:
>
> is_child($_GET['age'])
>
> The GET argument here should always be integer, however if it is in
> $_GET it will be a string.
>
> NB: I am not advocating the above to be a way of avoiding checking what
> there is in $_GET - however many PHP users will use it as such. If
> casting (with errors) help to avoid silent errors - then so much the
> better.
The GET argument, yes, should be an integer, but it's possible is not !
In this, actually i always use this syntaxe :
is_child(intval($_GET['age']));
or
is_child( (int) ($_GET['age']));
This a big precaution for security reason. And you say it below, many
users don't check there $_GET and $_POST datas.
Yes, casting could help us silently but, perhaps, this is not the better
way for educating these users.
And In this case especially, numeric type hiting work very fine.
The question is who coded the is_child function ?
1. The use himself ? If he use the type hiting in is function, he know
how use it.
2. It's a PHP core function ? The question is Type hiting or not in php
core function ? (perhaps in this case I prefer silently casting)
3. It's made by a team developper. Working with a team, there is
certainly some recommendation and guide line (include how to type hiting).
3. It's a downloaded librairie make by a third part ? This the third part
choice, there is certainly a good raison to be strict (or bad, just for
use type hiting for example).
>
>> And Exception is better than an Error because this give one chance to
>> programmer for resolving the problem before program be halted.
>
> Not everyone understands try/catch. Perhaps a user supplied
> function_argument_error() could be called if not in a try/catch block.
If user not understand try/catch, he don't use it and the exception, not
catched, generate an error.
Please, excuse my english, I'm very bad :)