zephir-lang / zephir

Zephir is a compiled high-level language aimed to ease the creation of C-extensions for PHP
https://zephir-lang.com
MIT License
3.29k stars 466 forks source link

Instanceof precedence #277

Open serebro opened 10 years ago

serebro commented 10 years ago

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

ovr commented 10 years ago

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

serebro commented 10 years ago

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.

phalcon commented 10 years ago

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)
nkt commented 10 years ago

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

ovr commented 10 years ago

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

nkt commented 10 years ago

@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