|
1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
2 | | -<!-- EN-Revision: e80ef2394f0c64be66917a5d4335736ae05b774f Maintainer: yannick Status: ready --> |
| 2 | +<!-- EN-Revision: f72c6031982a6f8152ef351d20f8e0d90e8f1612 Maintainer: lacatoire Status: ready --> |
3 | 3 | <!-- Reviewed: no --> |
4 | 4 | <refentry xml:id="function.http-build-query" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"> |
5 | 5 | <refnamediv> |
|
40 | 40 | Si <parameter>data</parameter> est un objet, alors seuls les |
41 | 41 | attributs publics seront utilisés dans le résultat. |
42 | 42 | </para> |
| 43 | + <note> |
| 44 | + <simpara> |
| 45 | + La méthode magique <link linkend="object.tostring">__toString()</link> |
| 46 | + n'est pas appelée lorsqu'un objet est évalué. Pour utiliser la |
| 47 | + représentation sous forme de chaîne d'un objet dans la chaîne de requête, |
| 48 | + l'objet doit être explicitement converti en chaîne. |
| 49 | + </simpara> |
| 50 | + </note> |
43 | 51 | </listitem> |
44 | 52 | </varlistentry> |
45 | 53 | <varlistentry> |
@@ -265,6 +273,44 @@ echo http_build_query($parent); |
265 | 273 | <screen> |
266 | 274 | <![CDATA[ |
267 | 275 | pub=publicParent&pub_bar%5Bpub%5D=publicChild |
| 276 | +]]> |
| 277 | + </screen> |
| 278 | + </example> |
| 279 | + |
| 280 | + <example> |
| 281 | + <title>Utilisation de <function>http_build_query</function> avec des objets contenant |
| 282 | + <link linkend="object.tostring">__toString()</link> |
| 283 | + </title> |
| 284 | + <programlisting role="php"> |
| 285 | +<![CDATA[ |
| 286 | +<?php |
| 287 | +class Foo { |
| 288 | + public $publicProperty = 'visible'; |
| 289 | +
|
| 290 | + public function __toString() { |
| 291 | + return "bar"; |
| 292 | + } |
| 293 | +} |
| 294 | +
|
| 295 | +$params = array( |
| 296 | + 'a' => 'b', |
| 297 | + 'foo' => new Foo() |
| 298 | +); |
| 299 | +
|
| 300 | +// Sans conversion, http_build_query lit les propriétés publiques |
| 301 | +echo http_build_query($params) . "\n"; |
| 302 | +
|
| 303 | +// Avec une conversion explicite, http_build_query utilise la sortie de __toString() |
| 304 | +$params['foo'] = (string) new Foo(); |
| 305 | +echo http_build_query($params) . "\n"; |
| 306 | +?> |
| 307 | +]]> |
| 308 | + </programlisting> |
| 309 | + &example.outputs; |
| 310 | + <screen> |
| 311 | +<![CDATA[ |
| 312 | +a=b&foo%5BpublicProperty%5D=visible |
| 313 | +a=b&foo=bar |
268 | 314 | ]]> |
269 | 315 | </screen> |
270 | 316 | </example> |
|
0 commit comments