Re: [RFC] Scalar Type Hinting With Casts (re-opening)

From: Date: Wed, 16 Jul 2014 23:32:43 +0000
Subject: Re: [RFC] Scalar Type Hinting With Casts (re-opening)
References: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17  Groups: php.internals 
Request: Send a blank email to internals+get-75615@lists.php.net to get a copy of this message
On 17 Jul 2014, at 00:12, Stas Malyshev <smalyshev@sugarcrm.com> wrote:

> I think if the compromise is having multiple set of rules for implicit
> casts then this compromise is not worth it. If you answer to the
> question of "what happens if I use a string in boolean context" with
> "well, it depends, if it's boolean context in syntax construct, it's one
> rule, if it's internal function, another, if it's user function, yet
> another" - it's not a good compromise. Any solution where you can give
> an actual answer like "empty string is false, all others are true" is
> much better. I'm not a fan of strict types in PHP, but having
> inconsistent rules is IMO so bad that even strict types would be better.
> At least you'd then know on which planet you are.

To be fair, the RFC as it stands has booleans be strict, with true and false the only allowed
values. Also, strings, integers and floats have (IMO) easy-to-explain rules under this RFC.:

* bools: bools and objects that convert to bools only
* strings: Anything that can be losslessly converted to a string works (any int, float, string, or
object with toString)
* float: Anything that can be losslessly converted to a float (any int, float, numeric-only string,
or object that converts to float)
* int: Anything that can be losslessly converted to an int (any int, float that’s integral,
integral numeric-only string, or object that converts to int)

Actually, objects are a special case here, unfortunately, but I didn’t really have a choice
because they cast weirdly.

This compares to the current zpp rules (as well as I can remember them, anyway):

* bools: Any int, string, float, bool, or object that can cast to bool; if it’s an int, then
non-zero is true; if it’s a float, then non-zero is true; if it’s a string, then it’s true
unless it’s an empty string or zero
* strings: Anything, unless it’s an object that doesn’t implement toString (I think that’s the
rule, anyway)
* int: Any int, float, bool, object that casts to int, or string that begins with numeric digits
(this is sometimes a notice)
* float: Any int, float, bool, object that casts to float, or string that begins with float-like
digits (this is sometimes a notice)

However, I honestly can’t remember zpp’s exact behaviour and I might have made some mistakes
there.

Is zend_parse_parameters’s behaviour actually documented in the manual? A quick search finds
nothing.

--
Andrea Faulds
http://ajf.me/






Thread (250 messages)

« previous php.internals (#75615) next »