Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > media > main > by-pkgid > 0afeee9cca140e167a996902b9a677c5 > files > 2969

php-manual-en-4.3.0-2mdk.noarch.rpm

<HTML
><HEAD
><TITLE
>extends</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="PHP Manual"
HREF="index.html"><LINK
REL="UP"
TITLE="Classes and Objects"
HREF="language.oop.html"><LINK
REL="PREVIOUS"
TITLE="Classes and Objects"
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 Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="language.oop.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>Chapter 14. Classes and Objects</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="language.oop.constructor.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="keyword.extends"
></A
><TT
CLASS="literal"
>extends</TT
></H1
><P
>&#13;    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
    substract 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"
><A
NAME="AEN5466"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>class Named_Cart extends Cart
{
    var $owner;
  
    function set_owner ($name)
    {
        $this-&#62;owner = $name;
    }
}</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    This defines a class Named_Cart that has all variables and
    functions of Cart plus an additional variable $owner 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"
><A
NAME="AEN5469"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>$ncart = new Named_Cart;    // Create a named cart
$ncart-&#62;set_owner("kris");  // Name that cart
print $ncart-&#62;owner;        // print the cart owners name
$ncart-&#62;add_item("10", 1);  // (inherited functionality from cart)</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
><P
>&#13;    This is also called a "parent-child" relationship. You create a class,
    parent, and use <TT
CLASS="literal"
>extends</TT
> 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
>Note: </B
>
     Classes must be defined before they are used! If you want the class
     <TT
CLASS="literal"
>Named_Cart</TT
> to extend the class
     <TT
CLASS="literal"
>Cart</TT
>, you will have to define the class
     <TT
CLASS="literal"
>Cart</TT
> first. If you want to create another class called
     <TT
CLASS="literal"
>Yellow_named_cart</TT
> based on the class
     <TT
CLASS="literal"
>Named_Cart</TT
> you have to define
     <TT
CLASS="literal"
>Named_Cart</TT
> 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"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="language.oop.constructor.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Classes and Objects</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="language.oop.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><TT
CLASS="literal"
>Constructors</TT
></TD
></TR
></TABLE
></DIV
></BODY
></HTML
>