Skip to content

Language: Traits, abstract classes, and interfaces #144

Description

@PurHur

Category

language

Problem (May 2026 refresh)

Simple trait use, abstract classes, and interfaces compile on VM today:

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php bin/vm.php -r "trait T { public function f(): int { return 1; } } class C { use T; } echo (new C)->f();"
php bin/vm.php -r "abstract class A { abstract public function f(): int; } class C extends A { public function f(): int { return 1; } } echo (new C)->f();"
php bin/vm.php -r "interface I { public function f(): int; } class C implements I { public function f(): int { return 1; } } echo (new C)->f();"
'

All print 1 on master. Remaining Zend parity:

  1. TraitUseAdaptationinsteadof / as fails at parse (Language: TraitUseAdaptation — insteadof / as alias (Zend compile parity) #3238)
  2. implements enforcement — missing method not fatal at compile time
  3. instanceof interface — verify bool against Zend (Compiler: Class inheritance (extends) and interfaces #101 overlap)
  4. JIT/AOT trait + abstract dispatch — VM-only today for some paths
  5. Horizontal trait conflicts — fatal when two traits define same method without adaptation
  6. Abstract instantiationnew AbstractClass must fatal

php-src reference

Repro — gaps

Trait adaptation (parse failure → #3238)

trait T { public function f(): int { return 1; } public function g(): int { return 2; } }
class C { use T { f as renamed; g insteadof f; } }

This compiler: parse error T_INSTEADOF
Zend: prints 12

Missing interface method (compile-time)

interface I { public function m(): void; }
class C implements I { }

Zend: compile error Class C contains 1 abstract method
This compiler: verify — file issue sub-task if silent

Abstract instantiate

abstract class A { public function f(): int { return 1; } }
new A();

Zend: fatal Cannot instantiate abstract class
This compiler: verify and match

Scope (this repo)

Layer Files Notes
Parser trait adaptation patch #3238
Compiler lib/Compiler.php compileClassLike Abstract + interface tables
VM lib/VM.php Trait merge, instanceof interface, abstract guard
JIT lib/JIT.php After VM PHPT green
Tests test/compliance/cases/trait_use.phpt, abstract_class.phpt, interface_implements.phpt

Done when

  • Simple use T; + abstract + interface repros remain green on VM
  • Missing interface method → compile error (Zend message subset)
  • new Abstract → fatal on VM
  • Trait conflict without adaptation → compile error
  • Language: TraitUseAdaptation — insteadof / as alias (Zend compile parity) #3238 trait adaptation tracked separately; link closed when landed
  • ./script/ci-fast.sh --filter TraitUse green
  • JIT/AOT: trait method call on @group llvm (or document VM fallback)

Verification

./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && ./script/ci-fast.sh --filter TraitUse'
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && vendor/bin/phpunit --filter trait'

Dependencies

Links

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:compilerCompiler / CFG / JITarea:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-2:languagePhase 2 – language features

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions