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: 2015-05-24 14:10 UTC
Votes:18
Avg. Score:4.4 ± 0.8
Reproduced:17 of 17 (100.0%)
Same Version:11 (64.7%)
Same OS:14 (82.4%)
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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dev dot php at ange7 dot com
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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-2019 The PHP Group
All rights reserved.
Last updated: Tue Jul 23 15:01:26 2019 UTC