Skip to content

http_build_query() skips Stringable params #10229

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

Open
procodix opened this issue Jan 5, 2023 · 0 comments · May be fixed by #10235
Open

http_build_query() skips Stringable params #10229

procodix opened this issue Jan 5, 2023 · 0 comments · May be fixed by #10235

Comments

@procodix
Copy link

procodix commented Jan 5, 2023

Description

The following code:

<?php 

$params = [
    'param1' => 1,
    'param2' => new Class {
        public function __toString():string {return 'test';}
    }
];

echo http_build_query($params);

foreach ($params as $k => &$v) $params[$k] = (string) $v;

echo http_build_query($params);

Resulted in this output:

param1=1

But I expected this output instead:

param1=1&param2=test

... as __toString() should have been implicitly called on the object. The ugly manual conversion shouldn't be necessary.

PHP Version

PHP 8.1

Operating System

Any

@Girgias Girgias self-assigned this Jan 5, 2023
Girgias added a commit to Girgias/php-src that referenced this issue Jan 5, 2023
The reason for this is that objects in the data set were treated like arrays and called recursively
Therefore we first check if the object is stringable in which case we handle it like a scalar,
otherwise we keep the existing behaviour of recursing through the object properties
@Girgias Girgias linked a pull request Jan 5, 2023 that will close this issue
Girgias added a commit to Girgias/php-src that referenced this issue Jan 8, 2023
The reason for this is that objects in the data set were treated like arrays and called recursively
Therefore we first check if the object is stringable in which case we handle it like a scalar,
otherwise we keep the existing behaviour of recursing through the object properties
Girgias added a commit to Girgias/php-src that referenced this issue Jan 13, 2023
The reason for this is that objects in the data set were treated like arrays and called recursively
Therefore we first check if the object is stringable in which case we handle it like a scalar,
otherwise we keep the existing behaviour of recursing through the object properties
Girgias added a commit to Girgias/php-src that referenced this issue Jan 15, 2023
The reason for this is that objects are treated like a key:value pair, like arrays, and called recursively
Therefore, if the object is Stringable and does not have any visible propertiese we handle it like a scalar.
Otherwise, we keep the existing key:value behaviour of recursing through the object properties.
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.

2 participants