Skip to content

Language: instanceof operator for class checks #138

@PurHur

Description

@PurHur

Problem

Expr_Instanceof_ is not handled in lib/Compiler.php (Unsupported expression). Web apps and templates eventually need guards like:

if ($handler instanceof Router) { /* … */ }

Not required for 003-MiniWebApp v1 (no instanceof in tree), but blocks framework-style routing and plugin checks.

Goal

$obj instanceof MyClass for simple named classes (no union types, no interfaces in v1).

class Box {}
$o = new Box();
var_export($o instanceof Box); // true
var_export($o instanceof stdClass); // false

VM + JIT + AOT parity with Zend for declared classes.

Implementation hints

Layer Files Notes
Compiler lib/Compiler.php compileExpr New opcode TYPE_INSTANCEOF or reuse class-id compare after #58 object headers
VM lib/VM.php Compare object ClassEntry name/id to rhs class literal
JIT lib/JIT.php, lib/JIT/Builtin/Type/Object_.php LLVM icmp on class id when objects JIT-supported
Lint lib/Lint/UnsupportedRegistry.php Map Expr_Instanceof until implemented
AOT lib/AOT/ Same metadata as VM class table

Tests

  • test/compliance/cases/language/instanceof_class.phpt
  • Negative: unrelated class name → false
  • @group llvm JIT/AOT stages in ci-local.sh

Acceptance criteria

docker run --rm -v "$(pwd):/compiler" -w /compiler php-compiler:22.04-dev \
  vendor/bin/phpunit test/compliance/cases/language/instanceof_class.phpt

Verification (local / Docker only)

./script/ci-fast.sh --filter instanceof

Dependencies

Priority

P2 — after #67 reference app and #58 OOP land.

Links

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions