📄 language.references.return.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Returning References</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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 which variable a reference should be bound to. Do <em class="emphasis">not</em> use return-by-reference to increase performance, the engine is smart enough to optimize this on its own. Only return references when you have a valid technical reason to do it! 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">getValue</var> function would be set, not the copy, as it would be without using reference syntax. </p> <blockquote><p><b class="note">Note</b>: <span class="simpara"> Unlike parameter passing, here you have to use <i>&</i> in both places - to indicate that you return by-reference, not a copy as usual, and to indicate that reference binding, rather than usual assignment, should be done for <var class="varname">$myValue</var>. </span> </p></blockquote> <blockquote><p><b class="note">Note</b>: <span class="simpara"> If you try to return a reference from a function with the syntax: <i>return ($this->value);</i> 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. <b><tt>E_NOTICE</tt></b> error is issued since PHP 4.4.0 and PHP 5.1.0 if the code tries to return a dynamic expression or a result of the <i>new</i> operator. </span> </p></blockquote> </div><hr /><div 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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -