Files
doc-fr/reference/overload/examples.xml
Yannick Torres 3d20fe648b &php; => PHP
git-svn-id: https://svn.php.net/repository/phpdoc/fr/trunk@261459 c90b9560-bf6c-de11-be94-00142212c4b1
2008-06-21 20:51:48 +00:00

91 lines
2.2 KiB
XML

<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.2 $ -->
<!-- EN-Revision: 1.2 Maintainer: yannick Status: ready -->
<!-- Reviewed: yes -->
<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>
Voici un exemple simple de fonctions utilisant <function>overload</function> :
<example>
<title>Overload avec une classe PHP</title>
<programlisting role="php">
<![CDATA[
<?php
class OO {
var $a = 111;
var $elem = array('b' => 9, 'c' => 42);
// Fonction de callback pour la lecture de membre
function __get($prop_name, &$prop_value)
{
if (isset($this->elem[$prop_name])) {
$prop_value = $this->elem[$prop_name];
return true;
} else {
return false;
}
}
// Fonction de callback pour l'écriture de membre
function __set($prop_name, $prop_value)
{
$this->elem[$prop_name] = $prop_value;
return true;
}
}
// Ici, l'initiation de l'overload
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:
// ajout d'une nouvelle valeur au membre $elem, en programmation OOP
$o->x = 56;
// instantiation de la classe stdclass (elle existe par défaut en PHP 4)
// $val n'est pas surchargée !
$val = new stdclass;
$val->prop = 555;
// Forcez "a" à être un tableau avec l'élément $val
// Mais _set() forcera cet élément dans le tableau $elem
$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
-->