migration51.references.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 260 行 · 第 1/2 页
HTML
260 行
&string(1) "c"}</pre></div> </pre></div> </div> <p class="para"> This is because, following the changes, <i>func()</i> assigns by value. The value of <var class="varname">$y</var> is re-assigned, and reference-binding is preserved from <var class="varname">$z</var>. Prior to the fix, the value was assigned by reference, leading <var class="varname">$y</var> to be re-bound on each assignment. The attempt to bind to a temporary value by reference was the cause of the memory corruption. </p> <p class="para"> Such code can be made to work identically in both the pre-fix and the post-fix PHP versions. The signature of <i>func()</i> can be altered to return by reference, or the reference assignment can be removed from the result of <i>func()</i>. </p> <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">func</span><span style="color: #007700">() {<br /> return </span><span style="color: #DD0000">'function return'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$x </span><span style="color: #007700">= </span><span style="color: #DD0000">'original value'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$y </span><span style="color: #007700">=& </span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$y </span><span style="color: #007700">= &</span><span style="color: #0000BB">func</span><span style="color: #007700">();<br />echo </span><span style="color: #0000BB">$x</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> In PHP 4.3 <var class="varname">$x</var> would be 'original value', whereas after the changes it would be 'function return' - remember that where the function does not return by reference, the reference assignment is converted to a regular assignment. Again, this can be brought to a common base, either by forcing <i>func()</i> to return by reference or by eliminating the by-reference assignment. </p> </div> <div id="migration51.references-error" class="section"> <h2 class="title">Code that worked under PHP 4.3.x, but now throws an error</h2> <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 /><br /> function </span><span style="color: #0000BB">getThis</span><span style="color: #007700">() {<br /> return </span><span style="color: #0000BB">$this</span><span style="color: #007700">;<br /> }<br /><br /> function </span><span style="color: #0000BB">destroyThis</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">$baz </span><span style="color: #007700">=& </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">getThis</span><span style="color: #007700">();<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= new </span><span style="color: #0000BB">Foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$bar</span><span style="color: #007700">-></span><span style="color: #0000BB">destroyThis</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$bar</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> In PHP 5.0.3, <var class="varname">$bar</var> evaluated to <b><tt>NULL</tt></b> instead of returning an object. That happened because <i>getThis()</i> returns by value, but the value here is assigned by reference. Although it now works in the expected way, this is actually invalid code which will throw an <b><tt>E_NOTICE</tt></b> under PHP 4.4 or an <b><tt>E_STRICT</tt></b> under PHP 5.0.4 and up. </p> </div> <div id="migration51.references-works" class="section"> <h2 class="title">Code that failed under PHP 4.3.x, but now works</h2> <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">f</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">$x </span><span style="color: #007700">= </span><span style="color: #DD0000">"foo"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$x</span><span style="color: #007700">);<br /> print </span><span style="color: #DD0000">"$x\n"</span><span style="color: #007700">;<br /> return(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br />}<br /><br />for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">< </span><span style="color: #0000BB">3</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br /> </span><span style="color: #0000BB">$h </span><span style="color: #007700">= &</span><span style="color: #0000BB">f</span><span style="color: #007700">();<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <p class="para"> In PHP 4.3 the third call to <a href="function.var-dump.html" class="function">var_dump()</a> produces <b><tt>NULL</tt></b>, due to the memory corruption caused by returning an uninitialized value by reference. This is valid code in PHP 5.0.4 and up, but threw errors in earlier releases of PHP. </p> </div> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$arr </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'a1' </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">'alfa' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'ok'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$arr </span><span style="color: #007700">=& </span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'a1'</span><span style="color: #007700">];<br />echo </span><span style="color: #DD0000">'-'</span><span style="color: #007700">.</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">'alfa'</span><span style="color: #007700">].</span><span style="color: #DD0000">"-\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <p class="para"> Until PHP 5.0.5, it wasn't possible to assign an array element by reference in this way. It now is. </p> </div> </div> <div id="migration51.references-didnotwork" class="section"> <h2 class="title">Code that <i>should have worked</i> under PHP 5.0.x</h2> <p class="para"> There are a couple of instances of bugs reported under PHP 5.0 prior to the reference fixes which now 'work'. However, in both cases errors are thrown by PHP 5.1.x, because the code was invalid in the first place. Returning values by reference using <i>self::</i> now works in the general case but throws an <b><tt>E_STRICT</tt></b> warning, and although your mileage may vary when assigning by reference to an overloaded object, you will still see an <b><tt>E_ERROR</tt></b> when you try it, even where the assignment itself appears to work. </p> </div> <div id="migration51.references-warnings" class="section"> <h2 class="title">Warnings that came and went</h2> <p class="para"> Nested calls to functions returning by reference are valid code under both PHP 4.3.x and PHP 5.1.x, but threw an unwarranted <b><tt>E_NOTICE</tt></b> or <b><tt>E_STRICT</tt></b> under the intervening PHP releases. </p> <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">foo</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">$var </span><span style="color: #007700">= </span><span style="color: #DD0000">'ok'</span><span style="color: #007700">;<br /> return </span><span style="color: #0000BB">$var</span><span style="color: #007700">;<br />}<br /><br />function & </span><span style="color: #0000BB">bar</span><span style="color: #007700">() {<br /> return </span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">=& </span><span style="color: #0000BB">bar</span><span style="color: #007700">();<br />echo </span><span style="color: #DD0000">"$a\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </div> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="migration51.html">Migrating from PHP 5.0.x to PHP 5.1.x</a></div> <div class="next" style="text-align: right; float: right;"><a href="migration51.reading.html">Reading []</a></div> <div class="up"><a href="migration51.html">Migrating from PHP 5.0.x to PHP 5.1.x</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?