<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Returning References</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.references.pass.html">Passing by Reference</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.references.unset.html">Unsetting References</a></div> <div class="up"><a href="language.references.html">References Explained</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><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 class="emphasis">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"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">foo </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 &</span><span style="color: #0000BB">getValue</span><span style="color: #007700">() {<br /> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></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">= &</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-></span><span style="color: #0000BB">getValue</span><span style="color: #007700">(); </span><span style="color: #FF8000">// $myValue is a reference to $obj->value, which is 42.<br /></span><span style="color: #0000BB">$obj</span><span style="color: #007700">-></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->value, i.e. 2.<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> In this example, the property of the object returned by the <var class="varname"><var class="varname">getValue</var></var> function would be set, not the copy, as it would be without using reference syntax. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> Unlike parameter passing, here you have to use <em>&</em> 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"><var class="varname">$myValue</var></var>. </span> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> If you try to return a reference from a function with the syntax: <em>return ($this->value);</em> this will <em class="emphasis">not</em> work as you are attempting to return the result of an <em class="emphasis">expression</em>, and not a variable, by reference. You can only return variables by reference from a function - nothing else. Since PHP 4.4.0 in the PHP 4 branch, and PHP 5.1.0 in the PHP 5 branch, an <strong><code>E_NOTICE</code></strong> error is issued if the code tries to return a dynamic expression or a result of the <em>new</em> operator. </span> </p></blockquote> <p class="para"> To use the returned reference, you must use reference assigment: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function &</span><span style="color: #0000BB">collector</span><span style="color: #007700">() {<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 /></span><span style="color: #0000BB">$collection </span><span style="color: #007700">= &</span><span style="color: #0000BB">collector</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$collection</span><span style="color: #007700">[] = </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> 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"><?php<br /></span><span style="color: #007700">function &</span><span style="color: #0000BB">collector</span><span style="color: #007700">() {<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 /></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 /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> Note that <em>array_push(&collector(), 'foo');</em> will <em class="emphasis">not</em> work, it results in a fatal error. </span> </p></blockquote> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.references.pass.html">Passing by Reference</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.references.unset.html">Unsetting References</a></div> <div class="up"><a href="language.references.html">References Explained</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>