Re: Allow arbitrary expressions when using instanceof operator

From: Date: Thu, 06 Nov 2014 09:14:20 +0000
Subject: Re: Allow arbitrary expressions when using instanceof operator
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-78800@lists.php.net to get a copy of this message
On Thu, 06 Nov 2014 13:01:52 +0400, Stas Malyshev <smalyshev@sugarcrm.com> wrote:
Hi!
What I want to implement is the ability to allow arbitrary expressions on the second operand, so instead of having to write something like this:
I'm afraid there's a problem with this. Arbitrary expressions include constants, right? So what this means: var_dump($foo instanceof Bar); is it checking $foo for being instance of class Bar (that's what is happening now) or is it taking the value of the constant Bar (whatever it is) - since constant is an expression - and using it as a class name and then checking if $foo is an instance of that class? You could of course require the expression to always be enclosed in (), but that produces weird syntax where some forms of instanceof work without () and some only with (). Given that you can easily assign your value to a variable, is it worth it? Also, you can always use is_a($foo, $bar->getClassName()).
Hey Stas, You're right about parenthesis - they are required to use expressions. Basically when you're in parenthesis - it will only mean that it's an expression, otherwise it's a direct class reference. It could be worked around by creating a new class of expression (on parser level, say expr_not_single_const or anyting like that) that would match any expression except constant references, but that's too much I guess. Also one thing not mentioned in the initial letter - this will also allow you to use expressions for new operator in the same way: `new (str_replace('/', '\\', $classPath))()` - just an example. I would say that it's just not logical to have some operator that can accept dynamic (runtime-defined) value but not an expression.

« previous php.internals (#78800) next »