Re: instanceof survives non-object variables, but crashes on non-object constants.
| From: | Michael Morris | Date: | Tue, 19 Dec 2017 22:09:55 +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-101398@lists.php.net to get a copy of this message | ||
What would it hurt to allow instanceof to return types other than object?
$x instanceof NULL
$x instanceof string
and so on. This would be more powerful and useful than throwing an error
where one wasn't being thrown before. It also would let one operator detect
all the types, rather than the plethora of detect functions we currently
have.
On Tue, Dec 19, 2017 at 4:48 PM, Nikita Popov <nikita.ppv@gmail.com> wrote:
> On Sat, Dec 9, 2017 at 7:28 AM, Andreas Hennings <andreas@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.
> >
>
> I've prepared a PR for this change:
> https://github.com/php/php-
> src/pull/2978
>
> From the discussion I'm understanding that our consensus is to implement
> this change, so if there are no further objection I'll merge this in a few
> days.
>
> Nikita
>