<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 > 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 > 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 > 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" ><?php class OO { var $a = 111; var $elem = array('b' => 9, 'c' => 42); // Callback method for getting a property function __get($prop_name, &$prop_value) { if (isset($this->elem[$prop_name])) { $prop_value = $this->elem[$prop_name]; return true; } else { return false; } } // Callback method for setting a property function __set($prop_name, $prop_value) { $this->elem[$prop_name] = $prop_value; return true; } } // Here we overload the OO object overload('OO'); $o = new OO; print "\$o->a: $o->a\n"; // print: $o->a: print "\$o->b: $o->b\n"; // print: $o->b: 9 print "\$o->c: $o->c\n"; // print: $o->c: 42 print "\$o->d: $o->d\n"; // print: $o->d: // add a new item to the $elem array in OO $o->x = 56; // instantiate stdclass (it is built-in in PHP 4) // $val is not overloaded! $val = new stdclass; $val->prop = 555; // Set "a" to be an array with the $val object in it // But __set() will put this in the $elem array $o->a = array($val); var_dump($o->a[0]->prop); ?></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 > 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 > -- 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 >