I think this is the main reason for differentiating between "strong" (or
whatever word is appropriate) and "weak." The developer may very well want
their script to blow-up in such a case.
I would liken this to the fact that we have both an "include" statement and
a "require" statement. One is recoverable, the other is not. I think the
same principle applies here.
--Kris
On Mon, Feb 27, 2012 at 2:31 PM, John Crenshaw <[email protected]>wrote:
> Inline
>
> > -----Original Message-----
> > From: Richard Lynch [mailto:[email protected]]
> >
> > On Mon, February 27, 2012 1:15 pm, Kris Craig wrote:
> > > Now, to rewind a bit past the latest chunk of "I hate this idea"
> > > posts....
> > >
> > > I'd like to suggest a new term: "strong".
> > >
> > > This term would be similar to "weak", except with a few key
> > > differences:
> > >
> > > - Weak would behave very much like Arvids suggested in his earlier
> > > post;
> > > i.e. if the variable is an integer but you pass a string (like
> > > "aaa") to
> > > it, a warning would be thrown and PHP would attempt to convert it
> > > (i.e. it
> > > would become 1).
> >
> > Nitpick:
> > Two backwards compatibility breaks:
> > 1) Throwing E_WARNING
> > 2) "aaa" converted to (int) is 0.
>
> Sorry, I don't understand the BC break on the warning. Old code doesn't
> have this anyway, so what exactly would break? Shouldn't all existing
> scripts continue to work exactly the same as before unless they add a type
> check?
>
> (I'm sure the example was just wrong, "aaa" should convert to 0)
>
> > > strong int $ii = "aaa"; // Throws a fatal error.
> >
> > Okay.
> >
> > E_FATAL seems drastic to me, but I'm not even going to use "strong" so
> don't really care either way, I suppose.
>
> I totally agree. E_RECOVERABLE_ERROR is the most severe that this should
> be (also matches the behavior of current type hints).
>
> > > weak int $i = "1"; // Converts to 1.
> >
> > Again, old-school (int) or set_type covers this.
>
> Unfortunately it doesn't completely. Yes, behavior is the same so long as
> the conversion is good, but a cast will silently accept impossible
> conversions, which is not what you really want here.
>
> John Crenshaw
> Priacta, Inc.
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>