Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8874

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>extends</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="keyword.class.html">class</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="oop4.constructor.html">Constructors</a></div>
 <div class="up"><a href="oop4.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"><em>extends</em></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;<em>extends</em>&#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"><var class="varname">$owner</var></var> and an
    additional function <em>set_owner()</em>. 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 <em>extends</em> 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 class="note"><p><strong class="note">Note</strong>: 
    <p class="para">
     Classes must be defined before they are used! If you want the class
     <em>Named_Cart</em> to extend the class
     <em>Cart</em>, you will have to define the class
     <em>Cart</em> first. If you want to create another class called
     <em>Yellow_named_cart</em> based on the class
     <em>Named_Cart</em> you have to define
     <em>Named_Cart</em> first. To make it short: the order in which
     the classes are defined is important.
    </p>
   </p></blockquote>
  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="keyword.class.html">class</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="oop4.constructor.html">Constructors</a></div>
 <div class="up"><a href="oop4.html">Classes and Objects (PHP 4)</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>