Unbacked enum %s cannot be converted to a string

Description

http_build_query() function convert an array of strings to their URL encoded version. When encountering an enumeration, it makes it work with string and integer enumerations, but not with the unbacked enumerations.

Example

<?php

enum E {
    case A;
}

try {
    echo http_build_query(['e' => E::A]);
} catch (ValueError $e) {
    echo $e->getMessage(), \n;
}

?>

Solutions

  • Add a type to the enumeration, string or int. Then, add values to the cases.

  • Convert the enumeration cases into strings before using them with http_build_query().

See Also