⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyword.extends.html

📁 php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>extends</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="language.oop.html">Classes and Objects (PHP 4)</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop.constructor.html">Constructors</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.extends" class="sect1">   <h2 class="title"><i>extends</i></h2>   <p class="para">    Often you need classes with similar variables and functions    to another existing class. In fact, it is good practice to    define a generic class which can be used in all your    projects and adapt this class for the needs of each of your    specific projects. To facilitate this, classes can be    extensions of other classes.  The extended or derived class    has all variables and functions of the base class (this is    called &#039;inheritance&#039; despite the fact that nobody died) and what    you add in the extended definition. It is not possible to    subtract from a class, that is, to undefine any existing     functions or variables. An extended class is always dependent    on a single base class, that is, multiple inheritance is    not supported. Classes are extended using the keyword &#039;extends&#039;.   </p>    <div class="informalexample">    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Named_Cart&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Cart&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;</span><span style="color: #0000BB">$owner</span><span style="color: #007700">;<br />&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">set_owner&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">owner&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>    <p class="para">    This defines a class Named_Cart that has all variables and functions of    Cart plus an additional variable <var class="varname">$owner</var> and an    additional function set_owner(). You create a named cart the usual way and    can now set and get the carts owner. You can still use normal cart    functions on named carts:   </p>    <div class="informalexample">    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$ncart&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Named_Cart</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Create&nbsp;a&nbsp;named&nbsp;cart<br /></span><span style="color: #0000BB">$ncart</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">set_owner</span><span style="color: #007700">(</span><span style="color: #DD0000">"kris"</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Name&nbsp;that&nbsp;cart<br /></span><span style="color: #007700">print&nbsp;</span><span style="color: #0000BB">$ncart</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">owner</span><span style="color: #007700">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;print&nbsp;the&nbsp;cart&nbsp;owners&nbsp;name<br /></span><span style="color: #0000BB">$ncart</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add_item</span><span style="color: #007700">(</span><span style="color: #DD0000">"10"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">);&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;(inherited&nbsp;functionality&nbsp;from&nbsp;cart)<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>   <p class="para">    This is also called a &quot;parent-child&quot; relationship. You create a class,    parent, and use <i>extends</i> to create a new class    <em class="emphasis">based</em> on the parent class: the child class. You can    even use this new child class and create another class based on this child    class.   </p>   <blockquote><p><b class="note">Note</b>:          Classes must be defined before they are used! If you want the class     <i>Named_Cart</i> to extend the class     <i>Cart</i>, you will have to define the class     <i>Cart</i> first. If you want to create another class called     <i>Yellow_named_cart</i> based on the class     <i>Named_Cart</i> you have to define     <i>Named_Cart</i> first. To make it short: the order in which     the classes are defined is important.    <br />   </p></blockquote>  </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop.html">Classes and Objects (PHP 4)</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop.constructor.html">Constructors</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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -