Re: make casts more strict in PHP 7
| From: | Mike Willbanks | Date: | Wed, 10 Sep 2014 02:01:45 +0000 |
| Subject: | Re: make casts more strict in PHP 7 | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-77127@lists.php.net to get a copy of this message | ||
> Hi!
>
> > No, no it would not. PHP’s explicit casts cannot fail, and there is
> > absolutely no good reason to change this. If people want strict
> > casting, we can add new functions or operators for that specifically.
> > But to break explicit casts and make them sometimes fail would cause
> > innumerable bugs and backwards-compatibility ideas.
>
> I agree. If the developer explicitly states they want conversion, it
> should be conversion. For implicit casts, some tightening may be ok, but
> for explicit ones I think it would bring much more headaches than it's
> worth.
>
Precisely, it would cause a vast amount of headaches for many projects.
Think of user input where you are expecting an integer and it must be a
positive integer (which would ultimately be a string); it is much faster to
do a quick check such as:
$foo = (int) $_GET['foo'];
if ($foo > 0) {
....
}
I'd personally not have to wrap more items around it due to a notice or
warning.