zephir icon indicating copy to clipboard operation
zephir copied to clipboard

Instanceof precedence

Open serebro opened this issue 10 years ago • 6 comments

I have the code: if !values instanceof \Traversable {}

and get this error: "InstanceOf requires a 'dynamic variable' in the left operand"

but it works if !(values instanceof \Traversable) {}

see http://www.php.net/manual/en/language.operators.precedence.php

serebro avatar Mar 23 '14 19:03 serebro

This bug is caused by left side not right! instance off success work with Traversable could you paste more code?

ovr avatar Mar 23 '14 22:03 ovr

No matter what class is used (Traversable, MyClass, etc). There is problem in the priority of operations. Before INSTANCEOF, after NOT. This is not essential, but in the feature may make a problem.

serebro avatar Mar 23 '14 23:03 serebro

This happens because !values returns a boolean value which cannot be used in a 'instanceof' operation.

PHP allows to do this because 'instanceof' has a difference precedence, but the code looks less than natural:

$a = null;
var_dump(!$a instanceof stdClass); // bool(true)

phalcon avatar Mar 23 '14 23:03 phalcon

In PHP instanceof has higher operator precedence than !. I think that is could be fixed, but that is not priority task, for now.

nkt avatar Mar 29 '14 09:03 nkt

i agree with @phalcon (it works like in php)

@nkt i don't think so write php code when instanceof would be higher than operator not

ovr avatar Mar 29 '14 09:03 ovr

@ovr please read comments in php manual about instanceof Actually phalcon write the same opinion.

PHP allows to do this because 'instanceof' has a difference precedence, but the code looks less than natural

Examples: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Constraints/CollectionValidator.php#L30 https://github.com/zendframework/zf2/blob/master/library/Zend/Db/Sql/Platform/AbstractPlatform.php#L64

nkt avatar Mar 29 '14 09:03 nkt