Re: instanceof survives non-object variables, but crashes on non-object constants.

From: Date: Sat, 09 Dec 2017 23:40:13 +0000
Subject: Re: instanceof survives non-object variables, but crashes on non-object constants.
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-101286@lists.php.net to get a copy of this message
On Sun, Dec 10, 2017 at 12:09 AM, Stanislav Malyshev <smalyshev@gmail.com> wrote: > Hi! > > > t think this behavior is inconsistent, and we should consider changing > it. > > > > There are two options, but only one is BC. > > > > - Let 1 instanceof \stdClass return false, instead of crashing. -> seems > BC > > - Let $x instanceof \stdClass crash, if $x is not an object. -> BC break. > > Neither of those "crashes", one of them returns fatal error, which is a > valid option for code that is obviously broken. It would also be a valid > option to return false, but since it is a valid outcome either way, I > don't think changing anything is necessary. It does not make any real > code easier to write (I can't see any legit reason to test numerics for > being instance of anything) and does not improve anything but abstract > "consistency", which in this case is not useful for anything. > The main issue seems to be that if people are not sure whether they have to do an is_object() check before using instanceof, they just quickly test it using "42 instanceof A" and then draw the entirely wrong conclusion from the generated error. I've certainly seen this play out more than once. Which is why I'd be in favor of changing instanceof to returning false in this case. Nikita

« previous php.internals (#101286) next »