keyword.parent.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 49 行
HTML
49 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>parent</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="keyword.paamayim-nekudotayim.html">Scope Resolution Operator (::)</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop.serialization.html">Serializing objects - objects in sessions</a></div> <div class="up"><a href="language.oop.html">Classes and Objects (PHP 4)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="keyword.parent" class="sect1"> <h2 class="title"><i>parent</i></h2> <p class="para"> You may find yourself writing code that refers to variables and functions in base classes. This is particularly true if your derived class is a refinement or specialisation of code in your base class. </p> <p class="para"> Instead of using the literal name of the base class in your code, you should be using the special name <i>parent</i>, which refers to the name of your base class as given in the <i>extends</i> declaration of your class. By doing this, you avoid using the name of your base class in more than one place. Should your inheritance tree change during implementation, the change is easily made by simply changing the <i>extends</i> declaration of your class. </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">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">example</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">"I am A::example() and provide basic functionality.<br />\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">B </span><span style="color: #007700">extends </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">example</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">"I am B::example() and provide additional functionality.<br />\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">example</span><span style="color: #007700">();<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= new </span><span style="color: #0000BB">B</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// This will call B::example(), which will in turn call A::example().<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">-></span><span style="color: #0000BB">example</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="keyword.paamayim-nekudotayim.html">Scope Resolution Operator (::)</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop.serialization.html">Serializing objects - objects in sessions</a></div> <div class="up"><a href="language.oop.html">Classes and Objects (PHP 4)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?