migration51.oop.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 142 行
HTML
142 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Class and object 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="migration51.integer-parameters.html">Integer values in function parameters</a></div> <div class="next" style="text-align: right; float: right;"><a href="migration51.extensions.html">Extensions</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.oop" class="section"> <h2 class="title">Class and object changes</h2> <ul class="itemizedlist"> <li class="listitem"> <p class="para"> <a href="migration51.oop.html#migration51.oop-functions" class="link"><i>instanceof</i>, <i>is_a()</i>, <i>is_subclass_of()</i> and <i>catch</i></a> </p> </li> <li class="listitem"> <p class="para"> <a href="migration51.oop.html#migration51.oop-methods" class="link">Abstract private methods</a> </p> </li> <li class="listitem"> <p class="para"> <a href="migration51.oop.html#migration51.oop-modifiers" class="link">Access modifiers in interfaces</a> </p> </li> <li class="listitem"> <p class="para"> <a href="migration51.oop.html#migration51.oop-inheritance" class="link">Changes in inheritance rules</a> </p> </li> <li class="listitem"> <p class="para"> <a href="migration51.oop.html#migration51.oop-constants" class="link">Class constants</a> </p> </li> </ul> <div id="migration51.oop-functions" class="section"> <h2 class="title"><i>instanceof</i>, <i>is_a()</i>, <i>is_subclass_of()</i> and <i>catch</i></h2> <p class="para"> In PHP 5.0, <i>is_a()</i> was deprecated and replaced by the <i>instanceof</i> operator. There were some issues with the initial implementation of <i>instanceof</i>, which relied on <i>__autoload()</i> to search for missing classes. If the class was not present, <i>instanceof</i> would throw a fatal <b><tt>E_ERROR</tt></b> due to the failure of <i>__autoload()</i> to discover that class. The same behaviour occurred in the <i>catch</i> operator and the <i>is_subclass_of()</i> function, for the same reason. </p> <p class="para"> None of these functions or operators call <i>__autoload()</i> in PHP 5.1.x, and the <i>class_exists()</i> workarounds used in code written for PHP 5.0.x, while not problematic in any way, are no longer necessary. </p> </div> <div id="migration51.oop-methods" class="section"> <h2 class="title">Abstract private methods</h2> <p class="para"> Abstract private methods were supported between PHP 5.0.0 and PHP 5.0.4, but were then disallowed on the grounds that the behaviours of <i>private</i> and <i>abstract</i> are mutually exclusive. </p> </div> <div id="migration51.oop-modifiers" class="section"> <h2 class="title">Access modifiers in interfaces</h2> <p class="para"> Under PHP 5.0, function declarations in interfaces were treated in exactly the same way as function declarations in classes. This has not been the case since October 2004, at which point only the <i>public</i> access modifier was allowed in interface function declarations. Since April 2005 - which pre-dates the PHP 5.0b1 release - the <i>static</i> modifier has also been allowed. However, the <i>protected</i> and <i>private</i> modifiers will now throw an <b><tt>E_ERROR</tt></b>, as will <i>abstract</i>. Note that this change should not affect your existing code, as none of these modifiers makes sense in the context of interfaces anyway. </p> </div> <div id="migration51.oop-inheritance" class="section"> <h2 class="title">Changes in inheritance rules</h2> <p class="para"> Under PHP 5.0, it was possible to have a function declaration in a derived class that did not match the declaration of the same function in the base class, e.g. </p> <div class="informalexample"> <p class="para"> This code will cause an <b><tt>E_STRICT</tt></b> error to be emitted under PHP 5.1.x. </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">Base </span><span style="color: #007700">{<br /> function &</span><span style="color: #0000BB">return_by_ref</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">$r </span><span style="color: #007700">= </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /> return </span><span style="color: #0000BB">$r</span><span style="color: #007700">;<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">Derived </span><span style="color: #007700">extends </span><span style="color: #0000BB">Base </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">return_by_ref</span><span style="color: #007700">() {<br /> return </span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /> }<br />}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </div> <div id="migration51.oop-constants" class="section"> <h2 class="title">Class constants</h2> <p class="para"> Under PHP 5.0.x, the following code was valid: </p> <div class="informalexample"> <p class="para"> Under PHP 5.1.x, redefinition of a class constant will throw a fatal <b><tt>E_ERROR</tt></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 /> const </span><span style="color: #0000BB">foobar </span><span style="color: #007700">= </span><span style="color: #DD0000">'foo'</span><span style="color: #007700">;<br /> const </span><span style="color: #0000BB">foobar </span><span style="color: #007700">= </span><span style="color: #DD0000">'bar'</span><span style="color: #007700">;<br />}<br /><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.integer-parameters.html">Integer values in function parameters</a></div> <div class="next" style="text-align: right; float: right;"><a href="migration51.extensions.html">Extensions</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 + -
显示快捷键?