migration51.references.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 260 行 · 第 1/2 页
HTML
260 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Changes in reference handling</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="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><hr /><div id="migration51.references" class="section"> <h2 class="title">Changes in reference handling</h2> <ul class="itemizedlist"> <li class="listitem"> <p class="para"> <a href="migration51.references.html#migration51.references-overview" class="link">Overview</a> </p> </li> <li class="listitem"> <p class="para"> <a href="migration51.references.html#migration51.references-fails" class="link">Code that worked under PHP 4.3.x, but now fails</a> </p> </li> <li class="listitem"> <p class="para"> <a href="migration51.references.html#migration51.references-error" class="link">Code that worked under PHP 4.3.x, but now throws an error</a> </p> </li> <li class="listitem"> <p class="para"> <a href="migration51.references.html#migration51.references-works" class="link">Code that failed under PHP 4.3.x, but now works</a> </p> </li> <li class="listitem"> <p class="para"> <a href="migration51.references.html#migration51.references-didnotwork" class="link">Code that <i>should have worked</i> under PHP 5.0.x</a> </p> </li> <li class="listitem"> <p class="para"> <a href="migration51.references.html#migration51.references-warnings" class="link">Warnings that came and went</a> </p> </li> </ul> <div id="migration51.references-overview" class="section"> <h2 class="title">Overview</h2> <p class="para"> From the PHP script writer's point of view, the change most likely to impact legacy code is in the way that references are handled in all PHP versions post-dating the PHP 4.4.0 release. </p> <p class="para"> Until and including PHP 4.3, it was possible to send, assign or return variables by reference that should really be returned by value, such as a constant, a temporary value (e.g. the result of an expression), or the result of a function that had itself been returned by value, as here: </p> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$foo </span><span style="color: #007700">= </span><span style="color: #DD0000">"123"</span><span style="color: #007700">;<br /><br />function </span><span style="color: #0000BB">return_value</span><span style="color: #007700">() {<br /> global </span><span style="color: #0000BB">$foo</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">$bar </span><span style="color: #007700">= &</span><span style="color: #0000BB">return_value</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> <p class="para"> Although this code would usually work as expected under PHP 4.3, in the general case the result is undefined. The Zend Engine could not act correctly on these values as references. This bug could and did lead to various hard-to-reproduce memory corruption problems, particularly where the code base was large. </p> <p class="para"> In PHP 4.4.0, PHP 5.0.4 and all subsequent PHP releases, the Engine was fixed to 'know' when the reference operation is being used on a value that should not be referenced. The actual value is now used in such cases, and a warning is emitted. The warning takes the form of an <b><tt>E_NOTICE</tt></b> in PHP 4.4.0 and up, and <b><tt>E_STRICT</tt></b> in PHP 5.0.4 and up. </p> <p class="para"> Code that could potentially produce memory corruption can no longer do so. However, some legacy code might work differently as a result. </p> </div> <div id="migration51.references-fails" class="section"> <h2 class="title">Code that worked under PHP 4.3, but now fails</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">func</span><span style="color: #007700">(&</span><span style="color: #0000BB">$arraykey</span><span style="color: #007700">) {<br /> return </span><span style="color: #0000BB">$arraykey</span><span style="color: #007700">; </span><span style="color: #FF8000">// function returns by value!<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">$array </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'a'</span><span style="color: #007700">, </span><span style="color: #DD0000">'b'</span><span style="color: #007700">, </span><span style="color: #DD0000">'c'</span><span style="color: #007700">);<br />foreach (</span><span style="color: #0000BB">array_keys</span><span style="color: #007700">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">) as </span><span style="color: #0000BB">$key</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">(</span><span style="color: #0000BB">$array</span><span style="color: #007700">[</span><span style="color: #0000BB">$key</span><span style="color: #007700">]);<br /> </span><span style="color: #0000BB">$z</span><span style="color: #007700">[] =& </span><span style="color: #0000BB">$y</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$z</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?><br /></span><</span></code></div> </div> <p class="para"> Running the above script under any version of PHP that pre-dates the reference fix would produce this output: </p> <div class="example-contents"><pre><div class="cdata"><pre>array(3) { [0]=> &string(1) "a" [1]=> &string(1) "b" [2]=> &string(1) "c"}</pre></div> </pre></div> <p class="para"> Following the reference fix, the same code would result in: </p> <div class="example-contents"><pre><div class="cdata"><pre>array(3) { [0]=> &string(1) "c" [1]=> &string(1) "c" [2]=>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?