Stas Malyshev wrote (on 14/07/2014):
But I'm not sure why you need this. You accept that if("foo")
works. Then why "function foo(bool $x) { if($x) { ... " should work
differently?
The answer - which is definitely a matter of opinion - is that allowing
any string reduces the usefulness of the type hint.
I realise there is not consensus on whether scalar hints should
represent validation, cast, or a mixture, but *if* we go down the route
of validation, then we have to choose which values are valid and which
are not.
My preference is to keep that strict: the example others have posted of
some_func($foo & BIT_FLAG) seems no more like a "real" boolean to me
than some_func(strlen($foo)) or any other expression which yields an
integer.
In fact, I'd find the behaviour more obvious if it were written
some_func((bool)$foo & BIT_FLAG) - it makes clear that some_func is not
itself aware of the flag, that's just the caller's way of making the
decision. That the type hint encouraged that would therefore seem like a
Good Thing.
(I'd post that comment on the appropriate sub-thread where that example
was raised, but don't have time to find it...)
Regards,
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php