php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #66966 __toString not call by http_build_query
Submitted: 2014-03-27 12:34 UTC Modified: 2023-05-06 08:22 UTC
Votes:21
Avg. Score:4.5 ± 0.8
Reproduced:19 of 20 (95.0%)
Same Version:11 (57.9%)
Same OS:15 (78.9%)
From: dev dot php at ange7 dot com Assigned:
Status: Verified Package: URL related
PHP Version: 5.6.9 OS: *
Private report: No CVE-ID: None
 [2014-03-27 12:34 UTC] dev dot php at ange7 dot com
Description:
------------
When you call http_build_query() method with array which contain one object, http_build_query don't call «__toString» method 

Test script:
---------------
<?php

class foo {

        public function __toString()
        {
                return "bar";
        }
}

$params = array();
$params['a'] = 'b';
$params['foo'] = new foo();

$query = http_build_query($params);

var_dump($params, $query);




Expected result:
----------------
a=b&foo=bar

Actual result:
--------------
a=b


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-03-27 12:40 UTC] dev dot php at ange7 dot com
-PHP Version: 5.5.10 +PHP Version: 5.5.9
 [2014-03-27 12:40 UTC] dev dot php at ange7 dot com
same bug with PHP 5.4.4-14+deb7u8
 [2014-06-24 15:07 UTC] dev dot php at ange7 dot com
Nobody has any interest about this bug...?
 [2014-07-28 07:39 UTC] martijn dot otto at copernica dot com
Is there any progress information available for this bug? It's pretty annoying since it means you always have to roll your own alternative to http_build_query.
 [2015-05-24 13:47 UTC] cmb@php.net
I can confirm the behavior: <http://3v4l.org/16dMa>. However, I'm
not sure whether this qualifies as bug in PHP (it seems to be more
like a feature request).

Anyhow, you could simply cast the object to string manually, see
<http://3v4l.org/eMb8J>.
 [2015-05-24 14:09 UTC] cmb@php.net
-Status: Open +Status: Verified -Operating System: Linux Debian Wheezy +Operating System: * -PHP Version: 5.5.9 +PHP Version: 5.6.9
 [2015-05-24 14:09 UTC] cmb@php.net
After having had a closer look at the implementation and the
documentation of http_build_query(), the behavior is not a bug,
but rather by design. The query string is build from arrays as
well as objects *recursively*. Example #4[1] demonstrates how that
works for nested objects.

Changing the behavior would be too much of a BC break for a small
gain (avoiding the manual cast to string). The documentation could
be improved however, to point out the behavior more clearly.
Therefore I'm changing to "Documentation problem".

[1] <http://php.net/manual/en/function.http-build-query.php#example-4523>
 [2015-05-24 14:10 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 04 08:01:29 2024 UTC