<?php
enum E: string { case A = 'x'; }
foreach ([
['hash', fn () => hash('sha256', E::A)],
['hash_hmac', fn () => hash_hmac('sha256', E::A, 'key')],
['md5', fn () => md5(E::A)],
['sha1', fn () => sha1(E::A)],
['crc32', fn () => crc32(E::A)],
] as [$name, $call]) {
try {
$call();
echo "$name: no throw\n";
} catch (Throwable $e) {
echo "$name: ", get_class($e), ': ', $e->getMessage(), "\n";
}
}
Problem
Digest builtins must reject enum case operands with
TypeError(PHP 8 typed signatures). This build coerces backed enum cases to strings/ints and computes a digest, sohash('sha256', E::A)silently succeeds.Related but separate:
TypeErrorfor array/object operands (todayLogicException)hash_equals()enum parity onlyphp-src reference
ext/hash/hash.c—php_hash_data,php_hash_hmacext/standard/basic_functions.c—php_md5,php_sha1,php_crc32IS_OBJECT/zend_enum— not coerced via string cast for these APIsRepro
php repro.php ./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && php bin/vm.php repro.php'hash('sha256', E::A)TypeError(arg #2$data)hash_hmac(..., E::A, 'key')TypeError(arg #2$data)md5(E::A)TypeError(arg #1$string)sha1(E::A)TypeErrorcrc32(E::A)TypeErrorScope (PHP-in-PHP)
ext/standard/VmHash.php,hash_.php,hash_hmac.phpVmString::coerceOperandext/standard/VmString.phpor dedicated guardmd5/sha1/crc32if routed through coercionlib/JIT/Prefer
EnumCaseSupport::isEnumCaseVariable()+TypeErrortemplates matching Zend message text.Done when
TypeErroron VM (and JIT/AOT where builtins are lowered)..phptundertest/compliance/cases/stdlib/.runtime/for this check.Related