Hello,

I was expected an RFC like this in PHP for a while. I'm happy somebody made
one, thanks.

But something hit me in that. even if you can't give an objet, you can give
any scalar type that will be cast.
I'm not sure this behavior is very relevant.

Actually if I ask for a string, why the user could be specify an int
without getting error ?

Consider the following PHP 5.6 code:

function foo($a) {
    if (!is_string($a)) {
        throw new \Exception('You need to give a string');
    }
}

Using your type hinting does not fix the problem of string checking because
the value will just be cast if it's an integer. Ofc if the user give an
object it's ok.
But why not throw a message error in any cases ?

I hope i'm clear enough.
Thanks for reading and have an happy new year :-) .


2015-01-01 13:41 GMT+01:00 Zeev Suraski <z...@zend.com>:

> > -----Original Message-----
> > From: Andrea Faulds [mailto:a...@ajf.me]
> > Sent: Wednesday, December 31, 2014 10:28 PM
> > To: PHP Internals
> > Subject: [PHP-DEV] [RFC] Scalar Type Hints
> >
> > Good evening,
> >
> > Parameter type hints for PHP’s scalar types are a long-requested feature
> > for
> > PHP. Today I am proposing an RFC which is a new attempt to add them to
> > the language. It is my hope that we can finally get this done for PHP 7.
>
> Andrea,
>
>
> I like this draft too, and that's a first after countless proposals over
> the
> last decade - so kudos! :)
>
> My main feedback here are the discrepancies between this RFC's casting
> rules
> and PHP's current built-in casting rules.  Ideally, I'd like those to be
> completely identical and not almost-identical.
> Since we're talking about v7.0, we do have the option of making changes to
> PHP's fundamental casting rules where appropriate (e.g. converting an array
> to a string).
> But that said, I think the way strings->numbers are handled - where they
> accept only numeric strings as it would mean you can't use casting to an
> int/float as an ultra-simple way to sanitize untrusted input.  I would
> change the † section from:
>
> †Non-numeric strings not accepted. Numeric strings with trailing characters
> are accepted, but produce a notice.
> to
> † Numeric strings with trailing characters and non-numeric strings are
> accepted, but produce a notice.
>
> - and apply it to both this RFC and the infrastructure convert_to_*(), so
> that it applies across the board in PHP.
>
> Zeev
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Reply via email to