<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.references.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'fr',
  ),
  'this' => 
  array (
    0 => 'language.references.return.php',
    1 => 'Retourner des r&eacute;f&eacute;rences',
    2 => 'Retourner des r&eacute;f&eacute;rences',
  ),
  'up' => 
  array (
    0 => 'language.references.php',
    1 => 'Les r&eacute;f&eacute;rences',
  ),
  'prev' => 
  array (
    0 => 'language.references.pass.php',
    1 => 'Passage par r&eacute;f&eacute;rence',
  ),
  'next' => 
  array (
    0 => 'language.references.unset.php',
    1 => 'D&eacute;truire une r&eacute;f&eacute;rence',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'fr',
    'path' => 'language/references.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.references.return" class="sect1">
  <h2 class="title">Retourner des références</h2>
  <p class="para">
   Retourner des références est utile lorsque l&#039;on
   souhaite utiliser une fonction pour déterminer à quelle variable
   une référence devrait être liée.
   Il ne faut <em>pas</em> utiliser
   le retour par référence pour améliorer les performances,
   le moteur est suffisamment robuste pour optimiser cela
   en interne. Il convient de ne retourner des références que
   lorsqu&#039;il y a de bonnes raisons techniques
   de le faire. Pour retourner des références, utiliser cette syntaxe :
   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">Foo<br /></span><span style="color: #007700">{<br />    public </span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #0000BB">42</span><span style="color: #007700">;<br /><br />    public function &amp;</span><span style="color: #0000BB">getValue</span><span style="color: #007700">()<br />    {<br />        return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">;<br />    }<br />}<br /><br /></span><span style="color: #0000BB">$obj </span><span style="color: #007700">= new </span><span style="color: #0000BB">Foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$myValue </span><span style="color: #007700">= &amp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getValue</span><span style="color: #007700">(); </span><span style="color: #FF8000">// $myValue est une référence de $obj-&gt;value, qui vaut 42.<br /></span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value </span><span style="color: #007700">= </span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">$myValue</span><span style="color: #007700">;                </span><span style="color: #FF8000">// affiche la nouvelle valeur de $obj-&gt;value, i.e. 2.<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   Dans cet exemple, on affecte une valeur à la propriété de l&#039;objet
   retourné par la fonction <var class="varname">getValue</var>, et non à sa copie,
   comme ce serait le cas si nous n&#039;avions pas utilisé la syntaxe de référence.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    Contrairement au passage de paramètre, ici, il faut utiliser
    <code class="literal">&amp;</code> aux deux endroits, à la fois pour
    indiquer le retour par référence (pas par copie), et
    pour indiquer l&#039;assignation aussi par référence (pas par copie
    non plus) pour la variable <var class="varname">$myValue</var>.
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    Tenter de retourner une référence depuis une fonction
    avec la syntaxe : <code class="literal">return ($this-&gt;value);</code>,
    ne fonctionnera <em>pas</em> comme
    attendu, et retournera le résultat de l&#039;<em>expression</em>,
    et pas de la variable, par référence. Seules
    des variables peuvent être retournées par référence depuis une fonction - et rien d&#039;autre.
   </span>
  </p></blockquote>
  <p class="para">
   Pour utiliser la référence retournée, il faut utiliser l&#039;affectation
   par référence :
   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function &amp;</span><span style="color: #0000BB">collector</span><span style="color: #007700">()<br />{<br />    static </span><span style="color: #0000BB">$collection </span><span style="color: #007700">= array();<br />    return </span><span style="color: #0000BB">$collection</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$collection </span><span style="color: #007700">= &amp;</span><span style="color: #0000BB">collector</span><span style="color: #007700">();<br /></span><span style="color: #FF8000">// Désormais, la variable $collection est une variable par référence qui référence le tableau static à l’intérieur de la fonction<br /><br /></span><span style="color: #0000BB">$collection</span><span style="color: #007700">[] = </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">collector</span><span style="color: #007700">());<br /></span><span style="color: #FF8000">// Array<br />// (<br />//    [0] =&gt; foo<br />// )<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <blockquote class="note"><p><strong class="note">Note</strong>: 
    <span class="simpara">
        Si l&#039;affectation est faite sans le symbole <code class="literal">&amp;</code>,
        par exemple <code class="code">$collection = collector();</code>,
        la variable <var class="varname">$collection</var> recevra une copie de la valeur,
        et non la référence retournée par la fonction.
    </span>
   </p></blockquote>
   Pour passer la référence retournée à une autre fonction attendant une référence,
   il est possible d&#039;utiliser la syntaxe suivante :
   <div class="informalexample">
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #007700">function &amp;</span><span style="color: #0000BB">collector</span><span style="color: #007700">()<br />{<br />    static </span><span style="color: #0000BB">$collection </span><span style="color: #007700">= array();<br />    return </span><span style="color: #0000BB">$collection</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">array_push</span><span style="color: #007700">(</span><span style="color: #0000BB">collector</span><span style="color: #007700">(), </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    Il est à noter que <code class="literal">array_push(&amp;collector(), &#039;foo&#039;);</code>
    <em>ne fonctionnera pas</em>, et résultera en une erreur
    fatale.
   </span>
  </p></blockquote>
 </div><?php manual_footer($setup); ?>