⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 language.references.return.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 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">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">42</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;&amp;</span><span style="color: #0000BB">getValue</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">foo</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$myValue&nbsp;</span><span style="color: #007700">=&nbsp;&amp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getValue</span><span style="color: #007700">();&nbsp;</span><span style="color: #FF8000">//&nbsp;$myValue&nbsp;is&nbsp;a&nbsp;reference&nbsp;to&nbsp;$obj-&gt;value,&nbsp;which&nbsp;is&nbsp;42.<br /></span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$myValue</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;prints&nbsp;the&nbsp;new&nbsp;value&nbsp;of&nbsp;$obj-&gt;value,&nbsp;i.e.&nbsp;2.<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><p><b class="note">Note</b>:     <span class="simpara">     Unlike parameter passing, here you have to use     <i>&amp;</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-&gt;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 + -