Sophie

Sophie

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

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

<HTML
><HEAD
><TITLE
>Object property and method call overloading</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="ob_start"
HREF="function.ob-start.html"><LINK
REL="NEXT"
TITLE="overload"
HREF="function.overload.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.ob-start.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="function.overload.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="reference"
><A
NAME="ref.overload"
></A
><DIV
CLASS="TITLEPAGE"
><H1
CLASS="title"
>LXXVI. Object property and method call overloading</H1
><DIV
CLASS="PARTINTRO"
><A
NAME="AEN70322"
></A
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="overload.intro"
></A
>Introduction</H1
><P
>&#13;     The purpose of this extension is to allow overloading of object
     property access and method calls. Only one function is defined
     in this extension, <A
HREF="function.overload.html"
><B
CLASS="function"
>overload()</B
></A
> which
     takes the name of the class that should have this functionality
     enabled. The class named has to define appropriate methods if
     it wants to have this functionality: <TT
CLASS="literal"
>__get()</TT
>,
     <TT
CLASS="literal"
>__set()</TT
> and <TT
CLASS="literal"
>__call()</TT
>
     respectively for getting/setting a property, or calling a method.
     This way overloading can be selective. Inside these handler
     functions the overloading is disabled so you can access object
     properties normally.
    </P
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>Warning</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>This extension is
<SPAN
CLASS="emphasis"
><I
CLASS="emphasis"
>EXPERIMENTAL</I
></SPAN
>. The behaviour of this extension --
including the names of its functions and anything else documented
about this extension -- may change without notice in a future release of PHP. 
Use this extension at your own risk.</P
></TD
></TR
></TABLE
></DIV
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="overload.requirements"
></A
>Requirements</H1
><P
>No external libraries are needed to build this extension.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="overload.installation"
></A
>Installation</H1
><P
>&#13;  In order to use these functions, you must compile
  PHP with the <TT
CLASS="option"
>--enable-overload</TT
> option.
  Starting with PHP 4.3.0 this extension is enabled by default. You can
  disable overload support with
  <TT
CLASS="option"
>--disable--overload</TT
>.
 </P
><P
> The windows version of <TT
CLASS="literal"
>PHP</TT
>
has built in support for this extension. You do not need to load any additional
extension in order to use these functions.</P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>Note: </B
>
   Builtin support for overload is available with PHP 4.3.0.
  </P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="overload.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="overload.resources"
></A
>Resource Types</H1
><P
>This extension has no resource types defined.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="overload.constants"
></A
>Predefined Constants</H1
><P
>This extension has no constants defined.</P
></DIV
><DIV
CLASS="section"
><H1
CLASS="section"
><A
NAME="overload.examples"
></A
>Examples</H1
><P
>&#13;     Some simple examples on using the <A
HREF="function.overload.html"
><B
CLASS="function"
>overload()</B
></A
>
     function:
     <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN70359"
></A
><P
><B
>Example 1. Overloading a PHP class</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><PRE
CLASS="php"
>&#60;?php

class OO
{
    var $a = 111;
    var $elem = array('b' =&#62; 9, 'c' =&#62; 42);

    // Callback method for getting a property
    function __get($prop_name, &#38;$prop_value)
    {
        if (isset($this-&#62;elem[$prop_name])) {
            $prop_value = $this-&#62;elem[$prop_name];
            return true;
        } else {
            return false;
        }
    }

    // Callback method for setting a property
    function __set($prop_name, $prop_value)
    {
        $this-&#62;elem[$prop_name] = $prop_value;
        return true;
    }
}

// Here we overload the OO object
overload('OO');

$o = new OO;
print "\$o-&#62;a: $o-&#62;a\n"; // print: $o-&#62;a:
print "\$o-&#62;b: $o-&#62;b\n"; // print: $o-&#62;b: 9
print "\$o-&#62;c: $o-&#62;c\n"; // print: $o-&#62;c: 42
print "\$o-&#62;d: $o-&#62;d\n"; // print: $o-&#62;d:

// add a new item to the $elem array in OO
$o-&#62;x = 56; 

// instantiate stdclass (it is built-in in PHP 4)
// $val is not overloaded!
$val = new stdclass;
$val-&#62;prop = 555;

// Set "a" to be an array with the $val object in it
// But __set() will put this in the $elem array
$o-&#62;a = array($val);
var_dump($o-&#62;a[0]-&#62;prop);

?&#62;</PRE
></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
    </P
><DIV
CLASS="warning"
><P
></P
><TABLE
CLASS="warning"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>Warning</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>&#13;      As this is an experimental extension, not all things
      work. There is no <TT
CLASS="literal"
>__call()</TT
> support
      currently, you can only overload the get and set
      operations for properties. You cannot invoke the
      original overloading handlers of the class, and
      <TT
CLASS="literal"
>__set()</TT
> only works to one level
      of property access.
     </P
></TD
></TR
></TABLE
></DIV
></DIV
></DIV
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
><A
HREF="function.overload.html"
>overload</A
>&nbsp;--&nbsp;Enable property and method call overloading for a class</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.ob-start.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.overload.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>ob_start</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="funcref.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>overload</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>