Bug #66966 [Opn->Ver]: __toString not call by http_build_query
Edit report at https://bugs.php.net/bug.php?id=66966&edit=1
ID: 66966
Updated by: cmb@php.net
Reported by: dev dot php at ange7 dot com
Summary: __toString not call by http_build_query
-Status: Open
+Status: Verified
Type: Bug
Package: URL related
-Operating System: Linux Debian Wheezy
+Operating System: *
-PHP Version: 5.5.9
+PHP Version: 5.6.9
Block user comment: N
Private report: N
New Comment:
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>
Previous Comments:
------------------------------------------------------------------------
[2015-05-24 13:47:50] 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>.
------------------------------------------------------------------------
[2014-07-28 07:39:35] 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.
------------------------------------------------------------------------
[2014-06-24 15:07:09] dev dot php at ange7 dot com
Nobody has any interest about this bug...?
------------------------------------------------------------------------
[2014-03-27 12:40:37] dev dot php at ange7 dot com
same bug with PHP 5.4.4-14+deb7u8
------------------------------------------------------------------------
[2014-03-27 12:34:46] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66966&edit=1
Thread (6 messages)