Skip to content

Zend: Make instanceof object equivalent to is_object()#22375

Open
dseguy wants to merge 1 commit into
php:masterfrom
dseguy:instance-object
Open

Zend: Make instanceof object equivalent to is_object()#22375
dseguy wants to merge 1 commit into
php:masterfrom
dseguy:instance-object

Conversation

@dseguy

@dseguy dseguy commented Jun 21, 2026

Copy link
Copy Markdown

$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.

`$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.

@TimWolla TimWolla left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable.

@ndossche ndossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dseguy dseguy force-pushed the instance-object branch from 927a3b0 to 3310fa3 Compare June 22, 2026 17:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants