<!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 'inheritance' 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 '<em>extends</em>'. </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">Named_Cart </span><span style="color: #007700">extends </span><span style="color: #0000BB">Cart </span><span style="color: #007700">{<br /> var </span><span style="color: #0000BB">$owner</span><span style="color: #007700">;<br /> <br /> function </span><span style="color: #0000BB">set_owner </span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">owner </span><span style="color: #007700">= </span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br /> }<br />}<br /></span><span style="color: #0000BB">?></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"><?php<br />$ncart </span><span style="color: #007700">= new </span><span style="color: #0000BB">Named_Cart</span><span style="color: #007700">; </span><span style="color: #FF8000">// Create a named cart<br /></span><span style="color: #0000BB">$ncart</span><span style="color: #007700">-></span><span style="color: #0000BB">set_owner</span><span style="color: #007700">(</span><span style="color: #DD0000">"kris"</span><span style="color: #007700">); </span><span style="color: #FF8000">// Name that cart<br /></span><span style="color: #007700">print </span><span style="color: #0000BB">$ncart</span><span style="color: #007700">-></span><span style="color: #0000BB">owner</span><span style="color: #007700">; </span><span style="color: #FF8000">// print the cart owners name<br /></span><span style="color: #0000BB">$ncart</span><span style="color: #007700">-></span><span style="color: #0000BB">add_item</span><span style="color: #007700">(</span><span style="color: #DD0000">"10"</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">); </span><span style="color: #FF8000">// (inherited functionality from cart)<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> This is also called a "parent-child" 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>