Skip to content

Language: enum method/static param default self $e = self::A compile-rejects (re-#9715, Zend/zend_compile.c) #22085

Description

@PurHur

Category

Language · php-src-strict · enum defaults (re-#9715 / #5885)

Problem

Zend allows enum cases as defaults when the parameter type is self (or the enum name) on enum methods/static methods. This compiler still rejects self $e = self::A at compile time with Cannot use object as default value for parameter … of type self, even though E $e = E::A on ordinary functions/classes already works (2026-07-22).

Repro Zend 8.2+ VM (2026-07-22)
enum E { case A; public static function f(self $e = self::A){ return $e->name; } } echo E::f(); A parseAndCompile failure: Cannot use object as default value for parameter $e of type self
function f(E $e = E::A) / class C { function f(E $e = E::A) } OK OK

php-src reference

PHP implementation target

  • lib/Compiler.php — when materializing parameter defaults, resolve self/static in enum scope to the enum class before the “object default” reject path (same path that already accepts E::A for named enum types)
  • Keep invalid cross-type defaults as compile errors per Zend
  • PHP-in-PHP; no new C

Repro

./script/docker-exec.sh -- bash -lc 'cat >/tmp/enum_self_default.php <<'"'"'P'"'"'
<?php
enum E {
  case A;
  case B;
  public function other(self $e = self::A): string {
    return $e->name;
  }
  public static function make(self $e = self::B): string {
    return $e->name;
  }
}
echo E::A->other(), "|", E::make(), "\n";
P
php /tmp/enum_self_default.php
php bin/vm.php /tmp/enum_self_default.php'

Done when

  • Repro prints A|B on VM (and JIT/AOT when in scope)
  • Ordinary E $e = E::A defaults remain green
  • Compliance .phpt under test/compliance/cases/language/
  • php-src-strict; no php-compiler-strict shortcut

Related

Activity

  1. added
    bugSomething isn't working
    area:vmVirtual machine
    implementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claim
    on Jul 22, 2026
  2. PurHur commented on Jul 22, 2026

    @PurHur
    OwnerAuthor

    claim: worker-lane-c — starting this run

  3. PurHur commented on Jul 22, 2026

    @PurHur
    OwnerAuthor

    Merged via #22093.

    Fix: resolve self/static param type hints to the declaring enum before compile-time default validation (assertCompileTimeDefaultMatchesDeclaredType).

    Verify:

    php bin/vm.php /tmp/enum_self_default.php  → A|B
    ./vendor/bin/phpunit test/compliance/EnumVMTest.php --filter enum_self_type_default_parameter  → OK
    
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 machinebugSomething isn't workingimplementation-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