<?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 => 'uk',
  ),
  'this' => 
  array (
    0 => 'language.references.return.php',
    1 => 'Returning References',
    2 => 'Returning References',
  ),
  'up' => 
  array (
    0 => 'language.references.php',
    1 => 'References Explained',
  ),
  'prev' => 
  array (
    0 => 'language.references.pass.php',
    1 => 'Passing by Reference',
  ),
  'next' => 
  array (
    0 => 'language.references.unset.php',
    1 => 'Unsetting References',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    '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">Returning References</h2>
   <p class="para">
    Returning by reference is useful when you want to use a function
    to find to which variable a reference should be bound. Do
    <em>not</em> use return-by-reference to increase performance.
    The engine will automatically optimize this on its own. Only return
    references when you have a valid technical reason to do so. To
    return references, use this syntax:
    <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 is a reference to $obj-&gt;value, which is 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">// Prints the new value of $obj-&gt;value, i.e. 2<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
     </div>

    </div>
    In this example, the property of the object returned by the
    <var class="varname">getValue</var> function would be set, not the
    copy, as it would be without using reference syntax.
   </p>
   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <span class="simpara">
     Unlike parameter passing, here you have to use
     <code class="literal">&amp;</code> in both places - to indicate that you
     want to return by reference, not a copy, and to indicate that
     reference binding, rather than usual assignment, should be done
     for <var class="varname">$myValue</var>.
    </span>
   </p></blockquote>
   <blockquote class="note"><p><strong class="note">Зауваження</strong>: 
    <span class="simpara">
     If you try to return a reference from a function with the syntax:
     <code class="literal">return ($this-&gt;value);</code> this will <em>not</em>
     work as you are attempting to return the result of an
     <em>expression</em>, and not a variable, by reference. You can
     only return variables by reference from a function - nothing else.
    </span>
   </p></blockquote>
   <p class="para">
   To use the returned reference, you must use reference assignment:
    <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">// Now the $collection is a referenced variable that references the static array inside the function<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">Зауваження</strong>: 
    <span class="simpara">
     If the assignment is done without the <code class="literal">&amp;</code> symbol, e.g. <code class="code">$collection = collector();</code>,
     the <var class="varname">$collection</var> variable will receive a copy of the value, not the reference returned by the function.
    </span>
   </p></blockquote>
   To pass the returned reference to another function expecting a reference
   you can use this syntax:
    <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">Зауваження</strong>: 
    <span class="simpara">
      Note that <code class="literal">array_push(&amp;collector(), &#039;foo&#039;);</code> will
      <em>not</em> work, it results in a fatal error.
    </span>
   </p></blockquote>
  </div><?php manual_footer($setup); ?>