Skip to content

BackedEnum support for http_build_query #15650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
taka-oyama opened this issue Aug 30, 2024 · 3 comments
Closed

BackedEnum support for http_build_query #15650

taka-oyama opened this issue Aug 30, 2024 · 3 comments

Comments

@taka-oyama
Copy link

Description

Currently, http_build_query converts BackedEnums into an object, which is not very useful.
I would like the result to be aligned with json_encode which returns the backed value.

<?php

enum Status: int
{
    case Active = 1;
}

$status = Status::Active;

echo http_build_query($status) . PHP_EOL; // "name=Active&value=1"
echo json_encode($status) . PHP_EOL; // "1"
@iluuu1994
Copy link
Member

I agree that the default behavior of http_build_query() is not very useful. Essentially, this is related to the fact that http_build_query() just does an array cast, and then concats the values. So, essentially the same as:

enum Foo: int {
    case Bar = 1;
}

var_dump((array) Foo::Bar);
array(2) {
  ["name"]=>
  string(3) "Bar"
  ["value"]=>
  int(1)
}

In general, enums are not their backing values, and conversion is explicit. This was a deliberate design decision of enums. I don't see much harm in handling them here though, although that poses the question why here and not in other places? Then again, we do have some special handling already, like json_encode(), as you pointed out.

@Crell Thoughts?

@Crell
Copy link
Contributor

Crell commented Aug 30, 2024

I'm of two minds here, I suppose.

  1. http_build_query() is a form of serialization, so using the backing value of an enum is reasonable and consistent.
  2. What in the hell are you doing passing an enum to http_build_query()? Why isn't that a type error?

Having the enum be a value of an array passed to http_build_query() would make more sense, I suppose, as I can see plenty of valid uses for that. So that leans toward option 1. Though passing an enum directly as the value to http_build_query() strikes me as something we shouldn't actually support. I don't know if it's feasible to do one and not the other.

@iluuu1994
Copy link
Member

I created an implementation here: #15704. Doesn't seem harmful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants