Category
language · php-src-strict
Problem
Casting a backed enum case to array must produce an associative array with name and value keys (case name + backing scalar). This compiler coerces the case to its backing scalar and wraps it in a list ([0 => backing]), which is wrong shape and wrong semantics.
php-src reference
Repro
test/repro/maintainer_gap_enum_array_cast.php:
<?php
enum E: int { case A = 1; case B = 2; }
var_export((array) E::A);
echo "\n";
var_export((array) E::B);
echo "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh
php test/repro/maintainer_gap_enum_array_cast.php
php bin/vm.php test/repro/maintainer_gap_enum_array_cast.php
'
| Engine |
(array) E::A |
| Zend PHP 8.2 |
array('name'=>'A','value'=>1) |
| VM today |
array(0=>1) ❌ |
Scope (PHP-in-PHP)
| Path |
Work |
lib/VM/EnumCaseSupport.php |
(array) cast handler for enum-case zvals |
lib/VM.php / cast opcodes |
Route CAST_ARRAY on enum cases |
lib/Compiler.php |
Compile-time (array) on known enum case if optimized |
| JIT/AOT |
Shared cast helper |
Done when (php-src-strict)
Related
Category
language· php-src-strictProblem
Casting a backed enum case to
arraymust produce an associative array withnameandvaluekeys (case name + backing scalar). This compiler coerces the case to its backing scalar and wraps it in a list ([0 => backing]), which is wrong shape and wrong semantics.php-src reference
Zend/zend_enum.c—zend_enum_cast_object/ array cast handlerZend/zend_operators.c— cast-to-array on object zvalsRepro
test/repro/maintainer_gap_enum_array_cast.php:(array) E::Aarray('name'=>'A','value'=>1)array(0=>1)❌Scope (PHP-in-PHP)
lib/VM/EnumCaseSupport.php(array)cast handler for enum-case zvalslib/VM.php/ cast opcodesCAST_ARRAYon enum caseslib/Compiler.php(array)on known enum case if optimizedDone when (php-src-strict)
var_exportmatches Zend for int- and string-backed enums(array)on case →array('name'=>'CaseName')onlytest/compliance/cases/language/enum_array_cast.phptgreenRelated