cvs: phpdoc-fr /language/oop5 magic.xml object-comparison.xml overloading.xml paamayim-nekudotayim.xml reflection.xml static.xml visibility.xml

From: Date: Sun, 24 Oct 2004 19:53:15 +0000
Subject: cvs: phpdoc-fr /language/oop5 magic.xml object-comparison.xml overloading.xml paamayim-nekudotayim.xml reflection.xml static.xml visibility.xml
Groups: php.doc.fr 
Request: Send a blank email to doc-fr+get-3529@lists.php.net to get a copy of this message
yannick Sun Oct 24 15:53:15 2004 EDT Modified files: /phpdoc-fr/language/oop5 magic.xml object-comparison.xml overloading.xml paamayim-nekudotayim.xml reflection.xml static.xml visibility.xml Log: sync with EN

http://cvs.php.net/diff.php/phpdoc-fr/language/oop5/magic.xml?r1=1.1&r2=1.2&ty=u Index: phpdoc-fr/language/oop5/magic.xml diff -u phpdoc-fr/language/oop5/magic.xml:1.1 phpdoc-fr/language/oop5/magic.xml:1.2 --- phpdoc-fr/language/oop5/magic.xml:1.1 Sun Jul 11 13:31:45 2004 +++ phpdoc-fr/language/oop5/magic.xml Sun Oct 24 15:53:14 2004 @@ -1,13 +1,129 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.1 $ --> -<!-- EN-Revision: 1.1 Maintainer: yannick Status: ready --> +<!-- $Revision: 1.2 $ --> +<!-- EN-Revision: 1.5 Maintainer: yannick Status: ready --> <sect1 id="language.oop5.magic"> <title>Méthodes magiques</title> <para> - . + Les fonctions nommées + <literal>__construct</literal>, + <literal>__destruct</literal> + (voir les <link linkend="language.oop5.decon">Constructeurs et Destructeurs</link>), + <literal>__call</literal>, + <literal>__get</literal>, + <literal>__set</literal> + (voir la <link linkend="language.oop5.overloading">surcharge</link>), + <literal>__sleep</literal>, + <literal>__wakeup</literal> et + <literal>__toString</literal> + sont magiques dans les classes &php;. Vous ne pouvez pas + avoir de fonctions nommées comme celà dans n'importe + la quelle de vos classes sans que vous ne vouliez modifier + le comportement associé à ces fonctions magiques. </para> + <caution> + <simpara> + &php; réserve toutes les fonctions dont le nom commence par __ pour + les fonctions magiques. Il est recommandé de ne pas utiliser de noms + de fonctions utilisant __ mise à part si vous voulez des fonctionnalités magiques + documentées. + </simpara> + </caution> + + <sect2 id="language.oop5.magic.sleep"> + <title><literal>__sleep</literal> et <literal>__wakeup</literal></title> + <para> + La fonction <function>serialize</function> vérifie si votre classe a une fonction + avec le nom magique <literal>__sleep</literal>. Si c'est le cas, cette fonction + sera exécutée avant toute linéarisation. Elle peut nettoyer l'objet et elle est supposée + retourner un tableau avec les noms de toutes les variables de l'objet + qui doivent être linéarisées. + </para> + <para> + Le but avoué de <literal>__sleep</literal> est de clôre toutes les + connexions aux bases de données possédés par l'objet, valider + les données en attente ou d'effectuer les nettoyages demandés. + De plus, cette fonction est utile si vous avez de très gros objets + qui n'ont pas besoin d'ere sauvegardés en totalité. + </para> + <para> + Réciproquement, la fonction <function>unserialize</function> vérifie + la présence d'une fonction dont le nom est le nom magique + <literal>__wakeup</literal>. Si elle est présente, cette fonction + peut reconstruire toute ressource que l'objet possède. + </para> + <para> + Le but avoué de <literal>__wakeup</literal> est de rétablir + toutes connexions aux bases de données qui ont été perdues + durant la linéarisation et d'effectuer toutes autres réinitialisations + demandées. + </para> + </sect2> + + <sect2 id="language.oop5.magic.tostring"> + <title><literal>__toString</literal></title> + <para> + La méthode <literal>__toString</literal> autorise une classe + de décider comme elle doit réagir lorsqu'elle est convertie en chaîne de + caractères. + </para> + <example> + <title>Exemple simple</title> + <programlisting role="php"> +<![CDATA[ +<?php +// Déclaration d'une classe simple +class TestClass +{ + public $foo; + + public function __construct($foo) { + $this->foo = $foo; + } + + public function __toString() { + return $this->foo; + } +} + +$class = new TestClass('Bonjour'); +echo $class; +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +Bonjour +]]> + </screen> + </example> + <para> + Il est important de noter que la méthode <literal>__toString</literal> + ne peut être appelée que lorsqu'elle est combiné avec + <function>echo</function> ou <function>print</function>. + </para> + <example> + <title>Cas où <literal>__toString</literal> est appelé</title> + <programlisting role="php"> +<![CDATA[ +<?php +// __toString appelé +echo $class; + +// __toString appelé (reste un paramètre normal pour echo) +echo 'text', $class; + +// __toString non appelé (opérateur de concaténation utilisé en premier) +echo 'text' . $class; +// __toString non appelé (convertion en une chaîne de caractères en premier) +echo (string) $class; +?> +]]> + </programlisting> + </example> + </sect2> </sect1> <!-- Keep this comment at the end of the file http://cvs.php.net/diff.php/phpdoc-fr/language/oop5/object-comparison.xml?r1=1.4&r2=1.5&ty=u Index: phpdoc-fr/language/oop5/object-comparison.xml diff -u phpdoc-fr/language/oop5/object-comparison.xml:1.4 phpdoc-fr/language/oop5/object-comparison.xml:1.5 --- phpdoc-fr/language/oop5/object-comparison.xml:1.4 Wed Aug 11 14:06:04 2004 +++ phpdoc-fr/language/oop5/object-comparison.xml Sun Oct 24 15:53:14 2004 @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.4 $ --> -<!-- EN-Revision: 1.2 Maintainer: yannick Status: ready --> +<!-- $Revision: 1.5 $ --> +<!-- EN-Revision: 1.3 Maintainer: yannick Status: ready --> <sect1 id="language.oop5.object-comparison"> <title>Comparaison d'objets</title> <para> @@ -26,7 +26,8 @@ <programlisting role='php'> <![CDATA[ <?php -function bool2str($bool) { +function bool2str($bool) +{ if ($bool === false) { return 'FALSE'; } else { @@ -34,25 +35,28 @@ } } -function compareObjects(&$o1, &$o2) { +function compareObjects(&$o1, &$o2) +{ echo 'o1 == o2 : '.bool2str($o1 == $o2)."\n"; echo 'o1 != o2 : '.bool2str($o1 != $o2)."\n"; echo 'o1 === o2 : '.bool2str($o1 === $o2)."\n"; echo 'o1 !== o2 : '.bool2str($o1 !== $o2)."\n"; } -class Flag { - var $flag; +class Flag +{ + public $flag; - function Flag($flag=true) { + function Flag($flag = true) { $this->flag = $flag; } } -class OtherFlag { - var $flag; +class OtherFlag +{ + public $flag; - function OtherFlag($flag=true) { + function OtherFlag($flag = true) { $this->flag = $flag; } } http://cvs.php.net/diff.php/phpdoc-fr/language/oop5/overloading.xml?r1=1.4&r2=1.5&ty=u Index: phpdoc-fr/language/oop5/overloading.xml diff -u phpdoc-fr/language/oop5/overloading.xml:1.4 phpdoc-fr/language/oop5/overloading.xml:1.5 --- phpdoc-fr/language/oop5/overloading.xml:1.4 Mon Aug 30 08:05:10 2004 +++ phpdoc-fr/language/oop5/overloading.xml Sun Oct 24 15:53:14 2004 @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.4 $ --> -<!-- EN-Revision: 1.4 Maintainer: yannick Status: ready --> +<!-- $Revision: 1.5 $ --> +<!-- EN-Revision: 1.6 Maintainer: yannick Status: ready --> <sect1 id="language.oop5.overloading"> <title>Surcharge</title> @@ -41,30 +41,33 @@ <programlisting role="php"> <![CDATA[ <?php -class Setter { +class Setter +{ public $n; private $x = array("a" => 1, "b" => 2, "c" => 3); - function __get($nm) { + function __get($nm) + { print "Récupération de [$nm]\n"; if (isset($this->x[$nm])) { $r = $this->x[$nm]; - print "Retour : $r\n"; + echo "Retour : $r\n"; return $r; } else { - print "Rien!\n"; + echo "Rien!\n"; } } - function __set($nm, $val) { + function __set($nm, $val) + { print "Définition de [$nm] à $val\n"; if (isset($this->x[$nm])) { $this->x[$nm] = $val; - print "OK!\n"; + echo "OK!\n"; } else { - print "Pas OK!\n"; + echo "Pas OK!\n"; } } } @@ -135,10 +138,12 @@ <programlisting role="php"> <![CDATA[ <?php -class Caller { +class Caller +{ private $x = array(1, 2, 3); - function __call($m, $a) { + function __call($m, $a) + { print "Méthode $m appelée :\n"; var_dump($a); return $this->x; http://cvs.php.net/diff.php/phpdoc-fr/language/oop5/paamayim-nekudotayim.xml?r1=1.7&r2=1.8&ty=u Index: phpdoc-fr/language/oop5/paamayim-nekudotayim.xml diff -u phpdoc-fr/language/oop5/paamayim-nekudotayim.xml:1.7 phpdoc-fr/language/oop5/paamayim-nekudotayim.xml:1.8 --- phpdoc-fr/language/oop5/paamayim-nekudotayim.xml:1.7 Sat Aug 28 05:53:00 2004 +++ phpdoc-fr/language/oop5/paamayim-nekudotayim.xml Sun Oct 24 15:53:14 2004 @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.7 $ --> -<!-- EN-Revision: 1.6 Maintainer: yannick Status: ready --> +<!-- $Revision: 1.8 $ --> +<!-- EN-Revision: 1.7 Maintainer: yannick Status: ready --> <sect1 id="language.oop5.paamayim-nekudotayim"> <title>L'opérateur de résolution de portée (::)</title> @@ -31,8 +31,9 @@ <![CDATA[ <?php class MyClass { - const CONST_VALUE = 'Une valeur constante'; + const CONST_VALUE = 'Une valeur constante'; } + echo MyClass::CONST_VALUE; ?> ]]> @@ -50,13 +51,14 @@ <programlisting role="php"> <![CDATA[ <?php -class OtherClass extends MyClass { - public static $my_static = 'variable statique'; - - public static function doubleColon() { - echo parent::CONST_VALUE . "\n"; - echo self::$my_static . "\n"; - } +class OtherClass extends MyClass +{ + public static $my_static = 'variable statique'; + + public static function doubleColon() { + echo parent::CONST_VALUE . "\n"; + echo self::$my_static . "\n"; + } } OtherClass::doubleColon(); @@ -80,21 +82,21 @@ <programlisting role="php"> <![CDATA[ <?php -class MyClass { - - protected function myFunc() { - echo "MyClass::myFunc()\n"; +class MyClass +{ + protected function myFunc() { + echo "MyClass::myFunc()\n"; } } -class OtherClass extends MyClass { - - /* Dépassement de la définition parent */ - public function myFunc() { - - /* Mais appel de la fonction parent */ - parent::myFunc(); - echo "OtherClass::myFunc()\n"; +class OtherClass extends MyClass +{ + // Dépassement de la définition parent + public function myFunc() { + + // Mais appel de la fonction parent + parent::myFunc(); + echo "OtherClass::myFunc()\n"; } } http://cvs.php.net/diff.php/phpdoc-fr/language/oop5/reflection.xml?r1=1.6&r2=1.7&ty=u Index: phpdoc-fr/language/oop5/reflection.xml diff -u phpdoc-fr/language/oop5/reflection.xml:1.6 phpdoc-fr/language/oop5/reflection.xml:1.7 --- phpdoc-fr/language/oop5/reflection.xml:1.6 Fri Aug 6 21:54:43 2004 +++ phpdoc-fr/language/oop5/reflection.xml Sun Oct 24 15:53:14 2004 @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.6 $ --> -<!-- EN-Revision: 1.2 Maintainer: yannick Status: ready --> +<!-- $Revision: 1.7 $ --> +<!-- EN-Revision: 1.5 Maintainer: yannick Status: ready --> <sect1 id="language.oop5.reflection"> <title>Réflexion</title> <sect2 id="language.oop5.reflection.introduction"> @@ -20,16 +20,16 @@ <programlisting role="php"> <![CDATA[ <?php - class Reflection { } - interface Reflector { } - class ReflectionException extends Exception { } - class ReflectionFunction implements Reflector { } - class ReflectionParameter implements Reflector { } - class ReflectionMethod extends ReflectionFunction { } - class ReflectionClass implements Reflector { } - class ReflectionObject extends ReflectionClass { } - class ReflectionProperty implements Reflector { } - class ReflectionExtension implements Reflector { } +class Reflection { } +interface Reflector { } +class ReflectionException extends Exception { } +class ReflectionFunction implements Reflector { } +class ReflectionParameter implements Reflector { } +class ReflectionMethod extends ReflectionFunction { } +class ReflectionClass implements Reflector { } +class ReflectionObject extends ReflectionClass { } +class ReflectionProperty implements Reflector { } +class ReflectionExtension implements Reflector { } ?> ]]> </programlisting> @@ -46,13 +46,12 @@ <programlisting role='php'> <![CDATA[ <?php - Reflection::export(new ReflectionClass('Exception')); +Reflection::export(new ReflectionClass('Exception')); ?> ]]> </programlisting> - </example> - Nous verrons : - <screen> + &example.outputs; + <screen> <![CDATA[ Class [ <internal> class Exception ] { @@ -104,7 +103,8 @@ } } ]]> - </screen> + </screen> + </example> </para> </sect2> @@ -118,22 +118,23 @@ <programlisting role="php"> <![CDATA[ <?php - class ReflectionFunction implements Reflector { - public object __construct(string name) - public string __toString() - public static string export() - public string getName() - public bool isInternal() - public bool isUserDefined() - public string getFileName() - public int getStartLine() - public int getEndLine() - public string getDocComment() - public array getStaticVariables() - public mixed invoke(mixed* args) - public bool returnsReference() - public ReflectionParameter[] getParameters() - } +class ReflectionFunction implements Reflector +{ + public object __construct(string name) + public string __toString() + public static string export() + public string getName() + public bool isInternal() + public bool isUserDefined() + public string getFileName() + public int getStartLine() + public int getEndLine() + public string getDocComment() + public array getStaticVariables() + public mixed invoke(mixed* args) + public bool returnsReference() + public ReflectionParameter[] getParameters() +} ?> ]]> </programlisting> @@ -215,16 +216,17 @@ <programlisting role="php"> <![CDATA[ <?php - class ReflectionParameter implements Reflector { - public object __construct(string name) - public string __toString() - public static string export() - public string getName() - public ReflectionClass getClass() - public bool allowsNull() - public bool isPassedByReference() - public bool isOptional() - } +class ReflectionParameter implements Reflector +{ + public object __construct(string name) + public string __toString() + public static string export() + public string getName() + public ReflectionClass getClass() + public bool allowsNull() + public bool isPassedByReference() + public bool isOptional() +} ?> ]]> </programlisting> @@ -246,34 +248,33 @@ <programlisting role='php'> <![CDATA[ <?php - function foo($a, $b, $c) { } - function bar(Exception $a, &$b, $c) { } - function baz(ReflectionFunction $a, $b = 1, $c = null) { } - function abc() { } - - // Création d'une instance de la classe Reflection_Function avec le - // paramètre fourni en ligne de commande. - $reflect = new ReflectionFunction($argv[1]); - - echo $reflect; - - foreach ($reflect->getParameters() as $i => $param) - { - printf( - "-- Paramètre #%d : %s {\n". - " Classe : %s\n". - " Autorise NULL : %s\n". - " Passé par référence : %s\n". - " Est optionnel ?: %s\n". - "}\n", - $i, - $param->getName(), - var_export($param->getClass(), 1), - var_export($param->allowsNull(), 1), - var_export($param->isPassedByReference(), 1), - $param->isOptional() ? 'oui' : 'non' - ); - } +function foo($a, $b, $c) { } +function bar(Exception $a, &$b, $c) { } +function baz(ReflectionFunction $a, $b = 1, $c = null) { } +function abc() { } + +// Création d'une instance de la classe Reflection_Function avec le +// paramètre fourni en ligne de commande. +$reflect = new ReflectionFunction($argv[1]); + +echo $reflect; + +foreach ($reflect->getParameters() as $i => $param) { + printf( + "-- Paramètre #%d : %s {\n". + " Classe : %s\n". + " Autorise NULL : %s\n". + " Passé par référence : %s\n". + " Est optionnel ?: %s\n". + "}\n", + $i, + $param->getName(), + var_export($param->getClass(), 1), + var_export($param->allowsNull(), 1), + var_export($param->isPassedByReference(), 1), + $param->isOptional() ? 'oui' : 'non' + ); +} ?> ]]> </programlisting> @@ -290,35 +291,36 @@ <programlisting role="php"> <![CDATA[ <?php - class ReflectionClass implements Reflector { - public __construct(string name) - public string __toString() - public static string export() - public string getName() - public bool isInternal() - public bool isUserDefined() - public string getFileName() - public int getStartLine() - public int getEndLine() - public string getDocComment() - public ReflectionMethod getConstructor() - public ReflectionMethod getMethod(string name) - public ReflectionMethod[] getMethods() - public ReflectionProperty getProperty(string name) - public ReflectionProperty[] getProperties() - public array getConstants() - public mixed getConstant(string name) - public bool isInstantiable() - public bool isInterface() - public bool isFinal() - public bool isAbstract() - public int getModifiers() - public bool isInstance(stdclass object) - public stdclass newInstance(mixed* args) - public ReflectionClass[] getInterfaces() - public ReflectionClass getParentClass() - public bool isSubclassOf(ReflectionClass class) - } +class ReflectionClass implements Reflector +{ + public __construct(string name) + public string __toString() + public static string export() + public string getName() + public bool isInternal() + public bool isUserDefined() + public string getFileName() + public int getStartLine() + public int getEndLine() + public string getDocComment() + public ReflectionMethod getConstructor() + public ReflectionMethod getMethod(string name) + public ReflectionMethod[] getMethods() + public ReflectionProperty getProperty(string name) + public ReflectionProperty[] getProperties() + public array getConstants() + public mixed getConstant(string name) + public bool isInstantiable() + public bool isInterface() + public bool isFinal() + public bool isAbstract() + public int getModifiers() + public bool isInstance(stdclass object) + public stdclass newInstance(mixed* args) + public ReflectionClass[] getInterfaces() + public ReflectionClass getParentClass() + public bool isSubclassOf(ReflectionClass class) +} ?> ]]> </programlisting> @@ -333,85 +335,84 @@ <programlisting role='php'> <![CDATA[ <?php - interface Serializable - { - // ... - } +interface Serializable +{ + // ... +} - class Object - { - // ... - } +class Object +{ + // ... +} - /** - * Une classe compteur - * - */ - class Counter extends Object implements Serializable - { - const START = 0; - private static $c = Counter::START; +/** + * Une classe compteur + * + */ +class Counter extends Object implements Serializable +{ + const START = 0; + private static $c = Counter::START; - /** - * Invocation du compteur - * - * @access public - * @return int - */ - public function count() - { - return self::$c++; - } - } + /** + * Invocation du compteur + * + * @access public + * @return int + */ + public function count() + { + return self::$c++; + } +} - // Création d'une instance de la classe ReflectionClass - $class= new ReflectionClass('Counter'); +// Création d'une instance de la classe ReflectionClass +$class= new ReflectionClass('Counter'); - // Affichage d'informations basiques - printf( - "===> La %s%s%s %s '%s' [extension de %s]\n". - " déclarée dans %s\n". - " lignes %d à %d\n". - " a le modificateur %d [%s]\n", - $class->isInternal() ? 'internal' : 'user-defined', - $class->isAbstract() ? ' abstract' : '', - $class->isFinal() ? ' final' : '', - $class->isInterface() ? 'interface' : 'class', - $class->getName(), - var_export($class->getParentClass(), 1), - $class->getFileName(), - $class->getStartLine(), - $class->getEndline(), - $class->getModifiers(), - implode(' ', Reflection::getModifierNames($class->getModifiers())) - ); +// Affichage d'informations basiques +printf( + "===> La %s%s%s %s '%s' [extension de %s]\n". + " déclarée dans %s\n". + " lignes %d à %d\n". + " a le modificateur %d [%s]\n", + $class->isInternal() ? 'internal' : 'user-defined', + $class->isAbstract() ? ' abstract' : '', + $class->isFinal() ? ' final' : '', + $class->isInterface() ? 'interface' : 'class', + $class->getName(), + var_export($class->getParentClass(), 1), + $class->getFileName(), + $class->getStartLine(), + $class->getEndline(), + $class->getModifiers(), + implode(' ', Reflection::getModifierNames($class->getModifiers())) +); - // Affichage du commentaire de la documentation - printf("---> Documentation:\n %s\n", var_export($class->getDocComment(), 1)); +// Affichage du commentaire de la documentation +printf("---> Documentation:\n %s\n", var_export($class->getDocComment(), 1)); - // Affichage de l'interface qui implémente cette classe - printf("---> Implémenté :\n %s\n", var_export($class->getInterfaces(), 1)); +// Affichage de l'interface qui implémente cette classe +printf("---> Implémenté :\n %s\n", var_export($class->getInterfaces(), 1)); - // Affichage des constantes de la classe - printf("---> Constantes : %s\n", var_export($class->getConstants(), 1)); +// Affichage des constantes de la classe +printf("---> Constantes : %s\n", var_export($class->getConstants(), 1)); - // Affichage des propriétés de la classe - printf("---> Properties: %s\n", var_export($class->getProperties(), 1)); +// Affichage des propriétés de la classe +printf("---> Properties: %s\n", var_export($class->getProperties(), 1)); - // Affichage des méthodes de la classe - printf("---> Méthodes : %s\n", var_export($class->getMethods(), 1)); +// Affichage des méthodes de la classe +printf("---> Méthodes : %s\n", var_export($class->getMethods(), 1)); - // Si cette classe est instanciable, création d'une instance - if ($class->isInstantiable()) - { - $counter= $class->newInstance(); +// Si cette classe est instanciable, création d'une instance +if ($class->isInstantiable()) { + $counter= $class->newInstance(); - echo '---> $counter est uneinstance ? '; - echo $class->isInstance($counter) ? 'oui' : 'non'; + echo '---> $counter est uneinstance ? '; + echo $class->isInstance($counter) ? 'oui' : 'non'; - echo "\n---> Le nouvel objet Object() est une instance ? "; - echo $class->isInstance(new Object()) ? 'oui' : 'non'; - } + echo "\n---> Le nouvel objet Object() est une instance ? "; + echo $class->isInstance(new Object()) ? 'oui' : 'non'; +} ?> ]]> </programlisting> @@ -442,33 +443,34 @@ <programlisting role="php"> <![CDATA[ <?php - class ReflectionMethod extends ReflectionFunction { - public __construct(mixed class, string name) - public static string export() - public mixed invoke(stdclass object, mixed* args) - public bool isFinal() - public bool isAbstract() - public bool isPublic() - public bool isPrivate() - public bool isProtected() - public bool isStatic() - public bool isConstructor() - public int getModifiers() - public ReflectionClass getDeclaringClass() - - /* Inherited from ReflectionFunction */ - public string __toString() - public string getName() - public bool isInternal() - public bool isUserDefined() - public string getFileName() - public int getStartLine() - public int getEndLine() - public string getDocComment() - public array getStaticVariables() - public bool returnsReference() - public ReflectionParameter[] getParameters() - } +class ReflectionMethod extends ReflectionFunction +{ + public __construct(mixed class, string name) + public static string export() + public mixed invoke(stdclass object, mixed* args) + public bool isFinal() + public bool isAbstract() + public bool isPublic() + public bool isPrivate() + public bool isProtected() + public bool isStatic() + public bool isConstructor() + public int getModifiers() + public ReflectionClass getDeclaringClass() + + // Inherited from ReflectionFunction + public string __toString() + public string getName() + public bool isInternal() + public bool isUserDefined() + public string getFileName() + public int getStartLine() + public int getEndLine() + public string getDocComment() + public array getStaticVariables() + public bool returnsReference() + public ReflectionParameter[] getParameters() +} ?> ]]> </programlisting> @@ -483,29 +485,29 @@ <programlisting role='php'> <![CDATA[ <?php - class Counter { - private static $c = 0; +class Counter +{ + private static $c = 0; - /** - * Incrémentation d'un compteur - * - * @final - * @static - * @access public - * @return int - */ - final public static function increment() - { - self::$c++; - return self::$c; - } - } + /** + * Incrémentation d'un compteur + * + * @final + * @static + * @access public + * @return int + */ + final public static function increment() + { + return self::$c; + } +} - // Création d'une instance de la classe Reflection_Method - $method= new ReflectionMethod('Counter', 'increment'); +// Création d'une instance de la classe Reflection_Method +$method= new ReflectionMethod('Counter', 'increment'); - // Affichage d'informations basiques - printf( +// Affichage d'informations basiques +printf( "===> La méthode %s%s%s%s%s%s%s '%s' (qui est %s)\n". " déclaré dans %s\n". " lignes %d à %d\n". @@ -526,18 +528,17 @@ implode(' ', Reflection::getModifierNames($method->getModifiers())) ); - // Affichage du commentaire de la documentation - printf("---> Documentation:\n %s\n", var_export($method->getDocComment(), 1)); +// Affichage du commentaire de la documentation +printf("---> Documentation:\n %s\n", var_export($method->getDocComment(), 1)); - // Affichage des variables statiques si elles existent - if ($statics= $method->getStaticVariables()) - { - printf("---> Variales statiques : %s\n", var_export($statics, 1)); - } +// Affichage des variables statiques si elles existent +if ($statics= $method->getStaticVariables()) { + printf("---> Variales statiques : %s\n", var_export($statics, 1)); +} - // Invocation de la méthode - printf("---> Résultat de l'invocation dans : "); - var_dump($method->invoke(NULL)); +// Invocation de la méthode +printf("---> Résultat de l'invocation dans : "); +var_dump($method->invoke(NULL)); ?> ]]> </programlisting> @@ -568,21 +569,22 @@ <programlisting role="php"> <![CDATA[ <?php - class ReflectionProperty implements Reflector { - public __construct(mixed class, string name) - public string __toString() - public static string export() - public string getName() - public bool isPublic() - public bool isPrivate() - public bool isProtected() - public bool isStatic() - public bool isDefault() - public int getModifiers() - public mixed getValue(stdclass object) - public void setValue(stdclass object, mixed value) - public ReflectionClass getDeclaringClass() - } +class ReflectionProperty implements Reflector +{ + public __construct(mixed class, string name) + public string __toString() + public static string export() + public string getName() + public bool isPublic() + public bool isPrivate() + public bool isProtected() + public bool isStatic() + public bool isDefault() + public int getModifiers() + public mixed getValue(stdclass object) + public void setValue(stdclass object, mixed value) + public ReflectionClass getDeclaringClass() +} ?> ]]> </programlisting> @@ -597,41 +599,41 @@ <programlisting role='php'> <![CDATA[ <?php - class String - { - public $length = 5; - } +class String +{ + public $length = 5; +} - // Création d'une instance de la classe ReflectionProperty - $prop = new ReflectionProperty('String', 'length'); +// Création d'une instance de la classe ReflectionProperty +$prop = new ReflectionProperty('String', 'length'); - // Affichage d'informations basiques - printf( - "===> Les propriétés %s%s%s%s '%s' (qui a %s)\n". - " a les modificateurs %s\n", - $prop->isPublic() ? ' public' : '', - $prop->isPrivate() ? ' private' : '', - $prop->isProtected() ? ' protected' : '', - $prop->isStatic() ? ' static' : '', - $prop->getName(), - $prop->isDefault() ? 'déclaré au moment de la compilation' : 'créé au moment de l\'exécution', - var_export(Reflection::getModifierNames($prop->getModifiers()), 1) - ); +// Affichage d'informations basiques +printf( + "===> Les propriétés %s%s%s%s '%s' (qui a %s)\n". + " a les modificateurs %s\n", + $prop->isPublic() ? ' public' : '', + $prop->isPrivate() ? ' private' : '', + $prop->isProtected() ? ' protected' : '', + $prop->isStatic() ? ' static' : '', + $prop->getName(), + $prop->isDefault() ? 'déclaré au moment de la compilation' : 'créé au moment de l\'exécution', + var_export(Reflection::getModifierNames($prop->getModifiers()), 1) +); - // Création d'une instance de String - $obj= new String(); +// Création d'une instance de String +$obj= new String(); - // Récupération de la valeur courante - printf("---> La veleur est : "); - var_dump($prop->getValue($obj)); - - // Modification de la valeur - $prop->setValue($obj, 10); - printf("---> Définition de la valeur à 10, la nouvelle valeur est : "); - var_dump($prop->getValue($obj)); +// Récupération de la valeur courante +printf("---> La veleur est : "); +var_dump($prop->getValue($obj)); + +// Modification de la valeur +$prop->setValue($obj, 10); +printf("---> Définition de la valeur à 10, la nouvelle valeur est : "); +var_dump($prop->getValue($obj)); - // Affichage de l'objet - var_dump($obj); +// Affichage de l'objet +var_dump($obj); ?> ]]> </programlisting> @@ -656,16 +658,17 @@ <programlisting role="php"> <![CDATA[ <?php - class ReflectionExtension implements Reflector { - public __construct(string name) - public string __toString() - public static string export() - public string getName() - public string getVersion() - public ReflectionFunction[] getFunctions() - public array getConstants() - public array getINIEntries() - } +class ReflectionExtension implements Reflector +{ + public __construct(string name) + public string __toString() + public static string export() + public string getName() + public string getVersion() + public ReflectionFunction[] getFunctions() + public array getConstants() + public array getINIEntries() +} ?> ]]> </programlisting> @@ -680,25 +683,25 @@ <programlisting role='php'> <![CDATA[ <?php - // Création d'une instance de la classe ReflectionProperty - $ext = new ReflectionExtension('standard'); +// Création d'une instance de la classe ReflectionProperty +$ext = new ReflectionExtension('standard'); - // Affichage d'informations basiques - printf( - "Nom : %s\n". - "Version : %s\n". - "Fonctions : [%d] %s\n". - "Constantes : [%d] %s\n". - "Entrées INI : [%d] %s\n", - $ext->getName(), - $ext->getVersion() ? $ext->getVersion() : 'NO_VERSION', - sizeof($ext->getFunctions()), - var_export($ext->getFunctions(), 1), - sizeof($ext->getConstants()), - var_export($ext->getConstants(), 1), - sizeof($ext->getINIEntries()), - var_export($ext->getINIEntries(), 1) - ); +// Affichage d'informations basiques +printf( + "Nom : %s\n". + "Version : %s\n". + "Fonctions : [%d] %s\n". + "Constantes : [%d] %s\n". + "Entrées INI : [%d] %s\n", + $ext->getName(), + $ext->getVersion() ? $ext->getVersion() : 'NO_VERSION', + sizeof($ext->getFunctions()), + var_export($ext->getFunctions(), 1), + sizeof($ext->getConstants()), + var_export($ext->getConstants(), 1), + sizeof($ext->getINIEntries()), + var_export($ext->getINIEntries(), 1) +); ?> ]]> </programlisting> @@ -718,37 +721,39 @@ <programlisting role='php'> <![CDATA[ <?php - /** - * Ma classe Reflection_Method - * - */ - class My_Reflection_Method extends ReflectionMethod { - public $visibility= ''; - - public function __construct($o, $m) { - parent::__construct($o, $m); - $this->visibility= Reflection::getModifierNames($this->getModifiers()); - } - } +/** + * Ma classe Reflection_Method + * + */ +class My_Reflection_Method extends ReflectionMethod +{ + public $visibility= ''; - /** - * Démo classe #1 - * - */ - class T { - protected function x() {} + public function __construct($o, $m) + { + parent::__construct($o, $m); + $this->visibility= Reflection::getModifierNames($this->getModifiers()); } +} - /** - * Démo classe #2 - * - */ - class U extends T { - function x() {} - } +/** + * Démo classe #1 + * + */ +class T { + protected function x() {} +} + +/** + * Démo classe #2 + * + */ +class U extends T { + function x() {} +} - // Affichage des informations - var_dump(new My_Reflection_Method('U', 'x')); +// Affichage des informations +var_dump(new My_Reflection_Method('U', 'x')); ?> ]]> </programlisting> http://cvs.php.net/diff.php/phpdoc-fr/language/oop5/static.xml?r1=1.3&r2=1.4&ty=u Index: phpdoc-fr/language/oop5/static.xml diff -u phpdoc-fr/language/oop5/static.xml:1.3 phpdoc-fr/language/oop5/static.xml:1.4 --- phpdoc-fr/language/oop5/static.xml:1.3 Fri Aug 13 07:21:28 2004 +++ phpdoc-fr/language/oop5/static.xml Sun Oct 24 15:53:14 2004 @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.3 $ --> -<!-- EN-Revision: 1.3 Maintainer: yannick Status: ready --> +<!-- $Revision: 1.4 $ --> +<!-- EN-Revision: 1.5 Maintainer: yannick Status: ready --> <sect1 id="language.oop5.static"> <title>Statique</title> @@ -15,7 +15,7 @@ La déclaration en statique doit être faite après la déclaration de visibilité. Pour des raisons de compatiblité avec &php; 4, si aucune déclaration de <link linkend="language.oop5.visibility">visibilité</link> n'est utilisée, - alors le membre ou la méthode sera traité comme si il avait été déclaré comme + alors le membre ou la méthode sera traité comme s'il avait été déclaré comme <literal>public static</literal>. </para> @@ -24,25 +24,42 @@ la pseudo variable <varname>$this</varname> n'est pas disponible dans la méthode déclarée en tant que statique. </para> + + <para> + En faite, les appels de méthodes <literal>static</literal> sont résolus + au moment de la compilation. Lorsqu'on utilise un nom de classe explicite, + la méthode est déjà identifiée complétement et aucune notion d'héritage + n'est appliquée. Si l'appel est effectuée par le mot clé <literal>self</literal>, + alors <literal>self</literal> est traduit en la classe courante, qui est la classe + appartenant au code. Ici aussi, aucune notion d'héritage + n'est appliquée. + </para> + + <para> + Les propritétés statiques ne peuvent être accédés à travers l'objet en utilisant + l'opérateur <literal>-&gt;</literal>. + </para> <example> <title>Exemple avec un membre statique</title> <programlisting role="php"> <![CDATA[ <?php -class Foo { - public static $my_static = 'foo'; - - public function staticValue() { - return self::$my_static; - } +class Foo +{ + public static $my_static = 'foo'; + + public function staticValue() { + return self::$my_static; + } } -class Bar extends Foo { +class Bar extends Foo +{ - public function fooStatic() { - return parent::$my_static; - } + public function fooStatic() { + return parent::$my_static; + } } @@ -50,7 +67,9 @@ $foo = new Foo(); print $foo->staticValue() . "\n"; -print $foo->my_static . "\n"; // my_static n'est pas défini +print $foo->my_static . "\n"; // propriété my_static non définie + +// $foo::my_static n'est pas possible print Bar::$my_static . "\n"; $bar = new Bar(); @@ -65,10 +84,11 @@ <programlisting role="php"> <![CDATA[ <?php -class Foo { - public static function aStaticMethod() { - // ... - } +class Foo +{ + public static function aStaticMethod() { + // ... + } } Foo::aStaticMethod(); http://cvs.php.net/diff.php/phpdoc-fr/language/oop5/visibility.xml?r1=1.6&r2=1.7&ty=u Index: phpdoc-fr/language/oop5/visibility.xml diff -u phpdoc-fr/language/oop5/visibility.xml:1.6 phpdoc-fr/language/oop5/visibility.xml:1.7 --- phpdoc-fr/language/oop5/visibility.xml:1.6 Fri Aug 13 07:21:28 2004 +++ phpdoc-fr/language/oop5/visibility.xml Sun Oct 24 15:53:14 2004 @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.6 $ --> -<!-- EN-Revision: 1.4 Maintainer: yannick Status: ready --> +<!-- $Revision: 1.7 $ --> +<!-- EN-Revision: 1.7 Maintainer: yannick Status: ready --> <sect1 id="language.oop5.visibility"> <title>Visibilité</title> <para> @@ -20,80 +20,145 @@ Les classes membres doivent être définies comme publiques, protégées ou privées. </para> + <para> <example> <title>Déclaration des membres</title> <programlisting role="php"> <![CDATA[ <?php - -class MyClass { - public $public = "MyClass::public!\n"; - protected $protected = "MyClass::Protected!\n"; - protected $protected2 = "MyClass::Protected2!\n"; - private $private = "MyClass::private!\n"; - - function printHello() { - print "MyClass::printHello() " . $this->private; - print "MyClass::printHello() " . $this->protected; - print "MyClass::printHello() " . $this->protected2; - } +/** + * Définition de MyClass + */ +class MyClass +{ + public $public = 'Public'; + protected $protected = 'Protected'; + private $private = 'Private'; + + function printHello() + { + echo $this->private; + echo $this->protected; + echo $this->private; + } } -class MyClass2 extends MyClass { - protected $protected = "MyClass2::protected!\n"; - - function printHello() { - - MyClass::printHello(); - - print "MyClass2::printHello() " . $this->public; - print "MyClass2::printHello() " . $this->protected; - print "MyClass2::printHello() " . $this->protected2; - - /* Fera une erreur fatale : */ - //print "MyClass2::printHello() " . $this->private; /* Erreur fatale */ - +$obj = new MyClass(); +echo $obj->public; // Fonctionne +echo $obj->protected; // Erreur fatale +echo $obj->private; // Erreur fatale +$obj->printHello(); // Affiche Public, Protected et Private + + +/** + * Définition de MyClass2 + */ +class MyClass2 extends MyClass +{ + // Nous pouvons déclarer les méthodes publiques et protégées, mais pas les privées + protected $protected = 'Protected'; + + function printHello() + { + echo $this->public; + echo $this->protected; + echo $this->private; } } -$obj = new MyClass(); - -print "Main:: " . $obj->public; -//print $obj->private; /* Erreur fatale */ -//print $obj->protected; /* Erreur fatale */ -//print $obj->protected2; /* Erreur fatale */ - -$obj->printHello(); /* doit s'afficher */ - $obj2 = new MyClass2(); -print "Main:: " . $obj2->private; /* Indéfini */ - -//print $obj2->protected; /* Erreur fatale */ -//print $obj2->protected2; /* Erreur fatale */ +echo $obj->public; // Fonctionne +echo $obj2->private; // Indéfini +echo $obj2->protected; // Erreur fatale +$obj2->printHello(); // Affiche Public, Protected2 et non Private -$obj2->printHello(); ?> ]]> </programlisting> </example> + </para> <note> + <simpara> - La déclaration de variables avec le mot clé 'var' en &php; 4 n'est plus - supportée pour les objets &php; 5. Pour des raisons de compatibilité, + La méthode de déclaration de variable en &php; 4 avec le mot + clé <emphasis>var</emphasis> n'est plus valide + pour les objets en &php; 5. Pour des raisons de compatibilité, une variable déclarée ainsi aura automatiquement une visibilité publique et une erreur de niveau <constant>E_STRICT</constant> sera générée. </simpara> + </note> </sect2> <sect2 id="language.oop5.visiblity-methods"> <title>Visibilité des méthodes</title> <para> - . + Les méthodes des classes doivent être définies en tant que + publiques, privées ou protégées. Les méthodes sans déclaration + seront automatiquement définies comme étant publiques. </para> - </sect2> + <para> + <example> + <title>Déclaration d'une méthode</title> + <programlisting role="php"> +<![CDATA[ +<?php +/** + * Définition de MyClass + */ +class MyClass +{ + // Les contructeurs doivent être publiques + public function __construct() { } + + // Déclaration d'une méthode publique + public function MyPublic() { } + + // Déclaration d'une méthode protégée + protected function MyProtected() { } + + // Déclaration d'une méthode privée + private function MyPrivate() { } + + // Celle-ci sera publique + function Foo() + { + $this->MyPublic(); + $this->MyProtected(); + $this->MyPrivate(); + } +} +$myclass = new MyClass; +$myclass->MyPublic(); // Fonctionne +$myclass->MyProtected(); // Erreur fatale +$myclass->MyPrivate(); // Erreur fatale +$myclass->Foo(); // Public, Protected et Private fonctionnent + + +/** + * Définition de MyClass2 + */ +class MyClass2 extends MyClass +{ + // Celle-ci sera publique + function Foo2() + { + $this->MyPublic(); + $this->MyProtected(); + $this->MyPrivate(); // Erreur fatale + } +} +$myclass2 = new MyClass2; +$myclass2->MyPublic(); // Fonctionne +$myclass2->Foo2(); // Public et Protected fonctionnent, non pas Private +?> +]]> + </programlisting> + </example> + </para> + </sect2> </sect1> <!-- Keep this comment at the end of the file
« previous php.doc.fr (#3529) next »