Zend: Make instanceof object equivalent to is_object()#22375
Conversation
`$x instanceof object` was syntactically valid but always returned false because `object` is a pseudo-type with no corresponding class entry in the class table. This made the construct silently unusable. At compile time, when the right-hand side of `instanceof` is the literal `object` pseudo-type, emit `ZEND_TYPE_CHECK` with `MAY_BE_OBJECT` instead of `ZEND_INSTANCEOF`. This is the same opcode used by `is_object()`, so `$x instanceof object` now behaves identically to `is_object($x)`. The fix is entirely at compile time; no VM handler changes are required.
There was a problem hiding this comment.
This reinforces an inconsistency in how pseudotypes are allowed to be used in different contexts. That should be resolved first.
In type declarations, object must be unqualified or you get a fatal error.
But this is still allowed in your PR: var_dump((new stdClass) instanceof \object);.
Also not a fan of allowing pseudotypes, one could make an argument that 3 instanceof int should also be supported; if instanceof object is supported.
Girgias
left a comment
There was a problem hiding this comment.
I agree with Nora, only supporting object introduces a new inconsistency. Something that would be resolved with @iluuu1994's pattern matching RFC introducing the new is keyword where you can check for types in a more generic way than instanceof.
Marking as requesting change as I do think this is contentious and would better be suited by a small RFC for people to properly establish semantics.
|
Fine with me. Let's close this and not make anyone else loose time on this. |
$x instanceof objectwas syntactically valid but always returned false becauseobjectis a pseudo-type with no corresponding class entry in the class table. This made the construct silently unusable.At compile time, when the right-hand side of
instanceofis the literalobjectpseudo-type, emitZEND_TYPE_CHECKwithMAY_BE_OBJECTinstead ofZEND_INSTANCEOF. This is the same opcode used byis_object(), so$x instanceof objectnow behaves identically tois_object($x).The fix is entirely at compile time; no VM handler changes are required.