RE: [PHP-DEV] instanceof survives non-object variables, but crashes on non-object constants.
| From: | Zeev Suraski | Date: | Sun, 10 Dec 2017 13:04:08 +0000 |
| Subject: | RE: [PHP-DEV] instanceof survives non-object variables, but crashes on non-object constants. | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-101292@lists.php.net to get a copy of this message | ||
> -----Original Message----> From: Niklas Keller [mailto:me@kelunik.com]
> Sent: Sunday, December 10, 2017 1:05 PM
> To: Sara Golemon <pollita@php.net>
> Cc: Andreas Hennings <andreas@dqxtech.net>; PHP internals
> <internals@lists.php.net>
> Subject: Re: [PHP-DEV] instanceof survives non-object variables, but crashes on
> non-object constants.
>
> >
> > This. There's nothing undefinable about "1 instanceof \stdClass", the
> > clear and obvious answer is: "No, it's not an instance of \stdClass,
> > it's an integer." The runtime result of false makes sense, the
> > compile time error doesn't. Let's relax the compile time error (fold
> > it into a const false if you truly must) and call it done. There's no
> > need to be hostile to users just because the code *seems* silly.
> >
>
> This code doesn't just *seem* silly. For everything but testing whether it works
> it is totally useless.
Do you mean the static use case specifically or in general? Because the runtime use case can
definitely be useful, even if it's not everyone's cup of tea.
I think it boils down to whether we want to:
(a) Keep things as-is.
(b) Block the runtime use case and error out in case of non-objects.
(c) Change the compile-time evaluation to allow non-objects and return false.
I definitely don't think we should do (b), as I completely agree with Sara the answer to
whether $x instanceof \stdClass when $x=1 is well defined, and is a clear 'no' with no
need to error out. It's also a perfectly reasonable behavior to rely on.
My vote goes to (c), because while (a) would prevent an admittedly silly piece of code (a hardcoded
check on whether an obviously non-object is an object) from running, I think consistency is
preferable, and I don't see how erroring out during compile time in this particular case buys
us anything.
Zeev