migration5.incompatible.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 183 行
HTML
183 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Backward Incompatible Changes</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="migration5.html">Migrating from PHP 4 to PHP 5.0.x</a></div> <div class="next" style="text-align: right; float: right;"><a href="migration5.cli-cgi.html">CLI and CGI</a></div> <div class="up"><a href="migration5.html">Migrating from PHP 4 to PHP 5.0.x</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="migration5.incompatible" class="section"> <h2 class="title">Backward Incompatible Changes</h2> <p class="para"> Although most existing PHP 4 code should work without changes, you should pay attention to the following backward incompatible changes: </p> <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> There are some <a href="reserved.html#reserved.keywords" class="link">new reserved keywords</a>. </span> </li> <li class="listitem"> <span class="simpara"> <a href="function.strrpos.html" class="function">strrpos()</a> and <a href="function.strripos.html" class="function">strripos()</a> now use the entire string as a needle. </span> </li> <li class="listitem"> <span class="simpara"> Illegal use of string offsets causes <b><tt>E_ERROR</tt></b> instead of <b><tt>E_WARNING</tt></b>. An example illegal use is: <i>$str = 'abc'; unset($str[0]);</i>. </span> </li> <li class="listitem"> <span class="simpara"> <a href="function.array-merge.html" class="function">array_merge()</a> was changed to accept only arrays. If a non-array variable is passed, a <b><tt>E_WARNING</tt></b> will be thrown for every such parameter. Be careful because your code may start emitting <b><tt>E_WARNING</tt></b> out of the blue. </span> </li> <li class="listitem"> <span class="simpara"> PATH_TRANSLATED server variable is no longer set implicitly under Apache2 SAPI in contrast to the situation in PHP 4, where it is set to the same value as the SCRIPT_FILENAME server variable when it is not populated by Apache. This change was made to comply with the <a href="http://hoohoo.ncsa.uiuc.edu/cgi/" class="link external">» CGI specification</a>. Please refer to <a href="http://bugs.php.net/23610" class="link external">» bug #23610</a> for further information, and see also the <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER['PATH_TRANSLATED']</a></var> description in the manual. This issue also affects PHP versions >= 4.3.2. </span> </li> <li class="listitem"> <span class="simpara"> The <b><tt>T_ML_COMMENT</tt></b> constant is no longer defined by the <a href="ref.tokenizer.html" class="link">Tokenizer</a> extension. If error_reporting is set to <b><tt>E_ALL</tt></b>, PHP will generate a notice. Although the <b><tt>T_ML_COMMENT</tt></b> was never used at all, it was defined in PHP 4. In both PHP 4 and PHP 5 // and /* */ are resolved as the <b><tt>T_COMMENT</tt></b> constant. However the PHPDoc style comments /** */, which starting PHP 5 are parsed by PHP, are recognized as <b><tt>T_DOC_COMMENT</tt></b>. </span> </li> <li class="listitem"> <span class="simpara"> <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER</a></var> should be populated with <var class="varname">argc</var> and <var class="varname">argv</var> if <a href="ini.core.html#ini.variables-order" class="link">variables_order</a> includes "S". If you have specifically configured your system to not create <var class="varname"><a href="reserved.variables.server.html" class="classname">$_SERVER</a></var>, then of course it shouldn't be there. The change was to always make <var class="varname">argc</var> and argv available in the CLI version regardless of the <a href="ini.core.html#ini.variables-order" class="link">variables_order</a> setting. As in, the CLI version will now always populate the global <var class="varname"><a href="reserved.variables.argc.html" class="classname">$argc</a></var> and <var class="varname"><a href="reserved.variables.argv.html" class="classname">$argv</a></var> variables. </span> </li> <li class="listitem"> <span class="simpara"> An object with no properties is no longer considered "empty". </span> </li> <li class="listitem"> <span class="simpara"> In some cases classes must be declared before use. It only happens if some of the new features of PHP 5 (such as <a href="language.oop5.interfaces.html" class="link">interfaces</a>) are used. Otherwise the behaviour is the old. </span> </li> <li class="listitem"> <span class="simpara"> <a href="function.get-class.html" class="function">get_class()</a>, <a href="function.get-parent-class.html" class="function">get_parent_class()</a> and <a href="function.get-class-methods.html" class="function">get_class_methods()</a> now return the name of the classes/methods as they were declared (case-sensitive) which may lead to problems in older scripts that rely on the previous behaviour (the class/method name was always returned lowercased). A possible solution is to search for those functions in all your scripts and use <a href="function.strtolower.html" class="function">strtolower()</a>. </span> <span class="simpara"> This case sensitivity change also applies to the <a href="language.constants.predefined.html" class="link">magical predefined constants</a> <b><tt>__CLASS__</tt></b>, <b><tt>__METHOD__</tt></b>, and <b><tt>__FUNCTION__</tt></b>. The values are returned exactly as they're declared (case-sensitive). </span> </li> <li class="listitem"> <span class="simpara"> <a href="function.ip2long.html" class="function">ip2long()</a> now returns <b><tt>FALSE</tt></b> when an invalid IP address is passed as argument to the function, and no longer <i>-1</i>. </span> </li> <li class="listitem"> <span class="simpara"> If there are functions defined in the included file, they can be used in the main file independent if they are before <b>return()</b> or after. If the file is included twice, PHP 5 issues fatal error because functions were already declared, while PHP 4 doesn't complain about it. It is recommended to use <b>include_once()</b> instead of checking if the file was already included and conditionally return inside the included file. </span> </li> <li class="listitem"> <span class="simpara"> <b>include_once()</b> and <b>require_once()</b> first normalize the path of included file on Windows so that including A.php and a.php include the file just once. </span> </li> </ul> <p class="para"> <div class="example"> <p><b>Example #1 <a href="function.strrpos.html" class="function">strrpos()</a> and <a href="function.strripos.html" class="function">strripos()</a> now use the entire string as a needle</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">strrpos</span><span style="color: #007700">(</span><span style="color: #DD0000">'ABCDEF'</span><span style="color: #007700">,</span><span style="color: #DD0000">'DEF'</span><span style="color: #007700">)); </span><span style="color: #FF8000">//int(3)<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">strrpos</span><span style="color: #007700">(</span><span style="color: #DD0000">'ABCDEF'</span><span style="color: #007700">,</span><span style="color: #DD0000">'DAF'</span><span style="color: #007700">)); </span><span style="color: #FF8000">//bool(false)<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #2 An object with no properties is no longer considered "empty"</b></p> <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">test </span><span style="color: #007700">{ }<br /></span><span style="color: #0000BB">$t </span><span style="color: #007700">= new </span><span style="color: #0000BB">test</span><span style="color: #007700">();<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(empty(</span><span style="color: #0000BB">$t</span><span style="color: #007700">)); </span><span style="color: #FF8000">// echo bool(false)<br /><br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$t</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">// Will be executed<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> <div class="example"> <p><b>Example #3 In some cases classes must be declared before used</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">//works with no errors:<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= new </span><span style="color: #0000BB">a</span><span style="color: #007700">();<br />class </span><span style="color: #0000BB">a </span><span style="color: #007700">{<br />}<br /><br /><br /></span><span style="color: #FF8000">//throws an error:<br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= new </span><span style="color: #0000BB">b</span><span style="color: #007700">();<br /><br />interface </span><span style="color: #0000BB">c</span><span style="color: #007700">{<br />}<br />class </span><span style="color: #0000BB">b </span><span style="color: #007700">implements </span><span style="color: #0000BB">c </span><span style="color: #007700">{<br />} <br /><br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="migration5.html">Migrating from PHP 4 to PHP 5.0.x</a></div> <div class="next" style="text-align: right; float: right;"><a href="migration5.cli-cgi.html">CLI and CGI</a></div> <div class="up"><a href="migration5.html">Migrating from PHP 4 to PHP 5.0.x</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?