<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >extends</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Handleiding" HREF="index.html"><LINK REL="UP" TITLE="Classes and Objects (PHP 4)" HREF="language.oop.html"><LINK REL="PREVIOUS" TITLE="Classes and Objects (PHP 4)" HREF="language.oop.html"><LINK REL="NEXT" TITLE="Constructors" HREF="language.oop.constructor.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Handleiding</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.oop.html" ACCESSKEY="P" >Terug</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Hoofdstuk 13. Classes and Objects (PHP 4)</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.oop.constructor.html" ACCESSKEY="N" >Volgende</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="keyword.extends" ><VAR CLASS="literal" >extends</VAR ></A ></H1 ><P > 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 'extends'. </P ><DIV CLASS="informalexample" ><P ></P ><A NAME="AEN4733" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">class </font><font color="#0000BB">Named_Cart </font><font color="#007700">extends </font><font color="#0000BB">Cart </font><font color="#007700">{<br /> var </font><font color="#0000BB">$owner</font><font color="#007700">;<br /> <br /> function </font><font color="#0000BB">set_owner </font><font color="#007700">(</font><font color="#0000BB">$name</font><font color="#007700">) {<br /> </font><font color="#0000BB">$this</font><font color="#007700">-></font><font color="#0000BB">owner </font><font color="#007700">= </font><font color="#0000BB">$name</font><font color="#007700">;<br /> }<br />}<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><P > 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" ><P ></P ><A NAME="AEN4737" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$ncart </font><font color="#007700">= new </font><font color="#0000BB">Named_Cart</font><font color="#007700">; </font><font color="#FF8000">// Create a named cart<br /></font><font color="#0000BB">$ncart</font><font color="#007700">-></font><font color="#0000BB">set_owner</font><font color="#007700">(</font><font color="#DD0000">"kris"</font><font color="#007700">); </font><font color="#FF8000">// Name that cart<br /></font><font color="#007700">print </font><font color="#0000BB">$ncart</font><font color="#007700">-></font><font color="#0000BB">owner</font><font color="#007700">; </font><font color="#FF8000">// print the cart owners name<br /></font><font color="#0000BB">$ncart</font><font color="#007700">-></font><font color="#0000BB">add_item</font><font color="#007700">(</font><font color="#DD0000">"10"</font><font color="#007700">, </font><font color="#0000BB">1</font><font color="#007700">); </font><font color="#FF8000">// (inherited functionality from cart)<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ><P ></P ></DIV ><P > This is also called a "parent-child" relationship. You create a class, parent, and use <VAR CLASS="literal" >extends</VAR > to create a new class <SPAN CLASS="emphasis" ><I CLASS="emphasis" >based</I ></SPAN > 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 ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Opmerking: </B > Classes must be defined before they are used! If you want the class <VAR CLASS="literal" >Named_Cart</VAR > to extend the class <VAR CLASS="literal" >Cart</VAR >, you will have to define the class <VAR CLASS="literal" >Cart</VAR > first. If you want to create another class called <VAR CLASS="literal" >Yellow_named_cart</VAR > based on the class <VAR CLASS="literal" >Named_Cart</VAR > you have to define <VAR CLASS="literal" >Named_Cart</VAR > first. To make it short: the order in which the classes are defined is important. </P ></BLOCKQUOTE ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.oop.html" ACCESSKEY="P" >Terug</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Begin</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.oop.constructor.html" ACCESSKEY="N" >Volgende</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Classes and Objects (PHP 4)</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.oop.html" ACCESSKEY="U" >Omhoog</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><VAR CLASS="literal" >Constructors</VAR ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >