Skip to content

Language: enum implements interface — parse + VM dispatch (Zend zend_enum.c parity) #3373

Description

@PurHur

Category

language

Problem

PHP 8.1+ allows enums to implement interfaces (enum Foo: string implements Bar { ... }). This compiler crashes during php-cfg parse when implements is present on an enum:

TypeError: PHPCfg\Op\Stmt\Enum_::__construct(): Argument #3 ($implements) must be of type array, PHPCfg\Block given

The vendor php-cfg Enum_ opcode and Parser::parseStmt_Enum() do not wire interface lists correctly (similar to class implements handling).

php-src reference

  • Zend/zend_enum.c — zend_register_enum, interface implementation on enum entries
  • Zend/zend_compile.c — zend_compile_enum implements-clause validation
  • ext/reflection/php_reflection.c — ReflectionEnum::implements() metadata

Repro (today)

<?php
interface HasName { public function label(): string; }

enum Status: string implements HasName {
    case Open = 'open';
    case Closed = 'closed';
    public function label(): string { return $this->name; }
}

echo Status::Open->label(), "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php repro.php'
/usr/bin/php repro.php   # Zend: Open

This compiler: fatal in php-cfg Parser.php before VM runs.

Scope (this repo)

Module Path
php-cfg vendor patch or fork: PHPCfg/Parser.php parseStmt_Enum() — pass interface name list to Enum_
Compiler store implements on enum class entry like user classes
VM instanceof / interface checks on enum instances
Tests test/compliance/cases/language/enum_implements_interface.phpt

Done when

Related

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