<?xml version="1.0" encoding="iso-8859-1"?> <!-- $Revision$ --> <appendix xml:id="overload.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"> &reftitle.examples; <section xml:id="overload.examples.basic"> <para> Some simple examples on using the <function>overload</function> function: <example> <title>Overloading a PHP class</title> <programlisting role="php"> <![CDATA[ <?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; echo "\$o->a: $o->a\n"; // print: $o->a: 111 echo "\$o->b: $o->b\n"; // print: $o->b: 9 echo "\$o->c: $o->c\n"; // print: $o->c: 42 echo "\$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); ?> ]]> </programlisting> </example> </para> </section> </appendix> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t indent-tabs-mode:nil sgml-parent-document:nil sgml-default-dtd-file:"../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->