Sophie

Sophie

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

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

<HTML
><HEAD
><TITLE
>Class/Object Functions</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="Function Reference"
HREF="funcref.html"><LINK
REL="PREVIOUS"
TITLE="com_set"
HREF="function.com-set.html"><LINK
REL="NEXT"
TITLE="call_user_method_array"
HREF="function.call-user-method-array.html"><META
HTTP-EQUIV="Content-type"
CONTENT="text/html; charset=ISO-8859-1"></HEAD
><BODY
CLASS="reference"
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="function.com-set.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.call-user-method-array.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="reference"
><A
NAME="ref.classobj"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="title"
>IX. Class/Object Functions</H1
><DIV
CLASS="PARTINTRO"
><A
NAME="AEN11527"
></A
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.intro"
></A
>Introduction</H1
><P
>&#13;      These functions allow you to obtain information about classes
      and instance objects. You can obtain the name of the class to
      which a object belongs, as well as its member properties and 
      methods. Using these functions, you can find out not only the
      class membership of an object, but also its parentage (i.e.
      what class is the object class extending).
     </P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.requirements"
></A
>Requirements</H1
><P
>No external libraries are needed to build this extension.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.installation"
></A
>Installation</H1
><P
>There is no installation needed to use these
functions; they are part of the PHP core.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.configuration"
></A
>Runtime Configuration</H1
><P
>This extension has no configuration directives defined in <TT
CLASS="filename"
>php.ini</TT
>.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.resources"
></A
>Resource Types</H1
><P
>This extension has no resource types defined.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.constants"
></A
>Predefined Constants</H1
><P
>This extension has no constants defined.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="classobj.examples"
></A
>Examples</H1
><P
>&#13;      In this example, we first define a base class and an extension
      of the class. The base class describes a general vegetable,
      whether it is edible or not and what is its color. The subclass
      <TT
CLASS="varname"
>Spinach</TT
> adds a method to cook it and another to
      find out if it is cooked.
     </P
><P
>&#13;      <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN11552"
></A
><P
><B
>Example 1. classes.inc</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php

// base class with member properties and methods
class Vegetable {

    var $edible;
    var $color;

    function Vegetable( $edible, $color="green" ) {
        $this-&#62;edible = $edible;
        $this-&#62;color = $color;
    }

    function is_edible() {
        return $this-&#62;edible;
    }

    function what_color() {
        return $this-&#62;color;
    }
    
} // end of class Vegetable

// extends the base class
class Spinach extends Vegetable {

    var $cooked = false;

    function Spinach() {
        $this-&#62;Vegetable( true, "green" );
    }

    function cook_it() {
        $this-&#62;cooked = true;
    }

    function is_cooked() {
        return $this-&#62;cooked;
    }
    
} // end of class Spinach

?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     </P
><P
>&#13;     We then instantiate 2 objects from these classes and print out
     information about them, including their class parentage.
     We also define some utility functions, mainly to have a nice printout
     of the variables.
     </P
><P
>&#13;      <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN11557"
></A
><P
><B
>Example 2. test_script.php</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;pre&#62;
&#60;?php

include "classes.inc";

// utility functions

function print_vars($obj) {
    $arr = get_object_vars($obj);
    while (list($prop, $val) = each($arr))
        echo "\t$prop = $val\n";
}

function print_methods($obj) {
    $arr = get_class_methods(get_class($obj));
    foreach ($arr as $method)
        echo "\tfunction $method()\n";
}

function class_parentage($obj, $class) {
    global $$obj;
    if (is_subclass_of($$obj, $class)) {
        echo "Object $obj belongs to class ".get_class($$obj);
        echo " a subclass of $class\n";
    } else {
        echo "Object $obj does not belong to a subclass of $class\n";
    }
}

// instantiate 2 objects

$veggie = new Vegetable(true,"blue");
$leafy = new Spinach();

// print out information about objects
echo "veggie: CLASS ".get_class($veggie)."\n";
echo "leafy: CLASS ".get_class($leafy);
echo ", PARENT ".get_parent_class($leafy)."\n";

// show veggie properties
echo "\nveggie: Properties\n";
print_vars($veggie);

// and leafy methods
echo "\nleafy: Methods\n";
print_methods($leafy);

echo "\nParentage:\n";
class_parentage("leafy", "Spinach");
class_parentage("leafy", "Vegetable");
?&#62;
&#60;/pre&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
     </P
><P
>&#13;      One important thing to note in the example above is that
      the object <TT
CLASS="varname"
>$leafy</TT
> is an instance of the class
      <B
CLASS="classname"
>Spinach</B
> which is a subclass of 
      <B
CLASS="classname"
>Vegetable</B
>,
      therefore the last part of the script above will output:
     </P
><P
>&#13;      <DIV
CLASS="informalexample"
><A
NAME="AEN11565"
></A
><P
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="programlisting"
>[...]
Parentage:
Object leafy does not belong to a subclass of Spinach
Object leafy belongs to class spinach a subclass of Vegetable</PRE
></TD
></TR
></TABLE
><P
></P
></DIV
>
     </P
></DIV
></DIV
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
><A
HREF="function.call-user-method-array.html"
>call_user_method_array</A
>&nbsp;--&nbsp;
     Call a user method given with an array of parameters [deprecated]
    </DT
><DT
><A
HREF="function.call-user-method.html"
>call_user_method</A
>&nbsp;--&nbsp;
     Call a user method on an specific object [deprecated]
    </DT
><DT
><A
HREF="function.class-exists.html"
>class_exists</A
>&nbsp;--&nbsp;Checks if the class has been defined</DT
><DT
><A
HREF="function.get-class-methods.html"
>get_class_methods</A
>&nbsp;--&nbsp;Returns an array of class methods' names</DT
><DT
><A
HREF="function.get-class-vars.html"
>get_class_vars</A
>&nbsp;--&nbsp;
     Returns an array of default properties of the class
    </DT
><DT
><A
HREF="function.get-class.html"
>get_class</A
>&nbsp;--&nbsp;Returns the name of the class of an object</DT
><DT
><A
HREF="function.get-declared-classes.html"
>get_declared_classes</A
>&nbsp;--&nbsp;Returns an array with the name of the defined classes</DT
><DT
><A
HREF="function.get-object-vars.html"
>get_object_vars</A
>&nbsp;--&nbsp;Returns an associative array of object properties</DT
><DT
><A
HREF="function.get-parent-class.html"
>get_parent_class</A
>&nbsp;--&nbsp;Retrieves the parent class name for object or class</DT
><DT
><A
HREF="function.is-a.html"
>is_a</A
>&nbsp;--&nbsp;
     Returns <TT
CLASS="constant"
><B
>TRUE</B
></TT
> if the object is of this class or has this class as 
     one of its parents
    </DT
><DT
><A
HREF="function.is-subclass-of.html"
>is_subclass_of</A
>&nbsp;--&nbsp;
     Returns <TT
CLASS="constant"
><B
>TRUE</B
></TT
> if the object has this class as one of its parents
    </DT
><DT
><A
HREF="function.method-exists.html"
>method_exists</A
>&nbsp;--&nbsp;Checks if the class method exists</DT
></DL
></DIV
></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="function.com-set.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="function.call-user-method-array.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>com_set</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="funcref.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>call_user_method_array</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>