The typical usage of `instanceof` is checking a `null|object` API that built an object of a (presumably known) type. Adding a crash there seems silly and overcomplicated.
On 9 Dec 2017 07:57, "Andreas Hennings" <andr...@dqxtech.net> wrote: > Why is the usage broken? It works :) > In practice, the current version of instanceof has an is_object() built-in. > This is what people could assume from trying. > > Of course I don't know how it is implemented internally. Maybe a kitten > dies every time I use instanceof on non-object variables? > > If we need an additional is_object() it can make some code more expensive.. > > > > > On 9 December 2017 at 07:46, Kalle Sommer Nielsen <ka...@php.net> wrote: > > > Hi > > > > That is fine for code that is broken in the first place. Similarly we > > added a warning some years back about array to string conversions. > > > > If your code is like the example, it should be updated and warned about > as > > you are doing something illogical by first checking the type and or value > > of the operand you are passing to instanceof after. > > > > The impact should be minimal as is, so persevering bc for broken usage is > > a poor argument imo > > > > > > > > On 9 Dec 2017 07.38, "Andreas Hennings" <andr...@dqxtech.net> wrote: > > > >> Adding a warning to something that used to be ok would break existing > >> code. > >> I remember a number of cases where I used instanceof on possibly > >> non-objects. > >> > >> if ($x instanceof C) { > >> return $x; > >> } > >> elseif ($x === NULL) { > >> ... > >> } > >> > >> All such code would then produce warnings. > >> > >> > >> On 9 December 2017 at 07:35, Kalle Sommer Nielsen <ka...@php.net> > wrote: > >> > >>> Hi > >>> > >>> We should just add a warning to the first example, it seems like an > >>> oversight that it was left silent > >>> > >>> On 9 Dec 2017 07.29 <20%2017%2007%2029>, "Andreas Hennings" < > >>> andr...@dqxtech.net> wrote: > >>> > >>>> The following (https://3v4l.org/A2Tp6) is ok, it simply returns > false: > >>>> > >>>> $x = 1; > >>>> $x instanceof \stdClass; > >>>> > >>>> > >>>> The following (https://3v4l.org/IdSBu) gives a fatal error: > >>>> > >>>> 1 instanceof \stdclass; > >>>> > >>>> 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. > >>>> > >>>> So it seems the first would the option we should take. > >>>> This is also what hhvm does, according to https://3v4l.org/IdSBu. > >>>> > >>> > >> >